Java 从现有 ZipFile 中删除条目

标签 java

我已经尝试了所有可能的方法并且我已经搜索了很多。我找到的唯一解决方案是这里 Adding (and overriding) files to compressed archive但是当我运行 zipFile.renameTo(tempFile) 行中的代码时,会发生一些事情,并且它会卡在那里。我不明白发生了什么?

有人可以帮助我吗?

        File zipFile = new File("C:/ass.zip");
                // get a temp file
                File tempFile = null;
                try {
                    tempFile = File.createTempFile(zipFile.getName(), null);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                // delete it, otherwise you cannot rename your existing zip to it.
                tempFile.delete();

                    System.out.println(zipFile.getAbsolutePath());
                    boolean renameOk=zipFile.renameTo(tempFile);
                    if (!renameOk)
                    {
                        try {
                            throw new Exception("could not rename the file "+zipFile.getAbsolutePath()+" to "+tempFile.getAbsolutePath());
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }


                byte[] buf = new byte[4096 * 1024];

                ZipInputStream zin = null;
                ZipEntry entry = null;
                ZipOutputStream out = null;

                try {
                    zin = new ZipInputStream(new FileInputStream(tempFile));
                    out = new ZipOutputStream(new FileOutputStream(zipFile));
                    entry = zin.getNextEntry();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                while (entry != null) {
                    String name = entry.getName();

                        if (entry.getName().contains("s.txt")) {
                               // Compress the files
                            InputStream in = null;
                            try {
                                in = new FileInputStream(entry.getName());
                                 String fName = entry.getName();
                                // Add ZIP entry to output stream.
                                out.putNextEntry(new ZipEntry(fName));
                            } catch (FileNotFoundException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }



                            // Transfer bytes from the file to the ZIP file
                            int len;
                            try {
                                while ((len = in.read(buf)) > 0) {
                                     String s = new String(buf);
                                     if (s.contains("host=")) {
                                         buf = s.replaceAll("host=sssws", "host="+PropertyHandler.getInstance().getProperty("hostName")).getBytes();
                                     }
                                    out.write(buf, 0, (len < buf.length) ? len : buf.length);
                                }
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                            // Complete the entry
                            try {
                                out.closeEntry();
                                 in.close();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }else{

                        // Add ZIP entry to output stream.
                        try {
                            out.putNextEntry(new ZipEntry(name));
                              // Transfer bytes from the ZIP file to the output file
                            int len;
                            while ((len = zin.read(buf)) > 0) {
                                try {
                                    out.write(buf, 0, len);
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }
                    try {
                        entry = zin.getNextEntry();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                // Close the streams
                try {
                    zin.close();
                     // Complete the ZIP file
                    out.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                tempFile.delete();

            }

最佳答案

你应该放置

tempFile.delete();

重命名 zipFile 后的这一行

boolean renameOk=zipFile.renameTo(tempFile);

所以就像这样

boolean renameOk=zipFile.renameTo(tempFile);
tempFile.delete();

关于Java 从现有 ZipFile 中删除条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34177835/

相关文章:

java - 截图 - 后台服务

java - 防止 gmail 将内联图像显示为附件

java - 如何使用 spring data jpa 仅更新模型中的传入字段?

java - 如何在 Jasper Reports/JRXML 中进行复杂计算

java - Java BlockingQueue 中的 put(s) 和 take(s)

java - 将两个数组列表合二为一

java - 使用 Visual Studio Code 将 Java Web 应用程序部署到 Tomcat

java - 如何在 JBoss 7 下为 war 指定安全域?

java - 我的 ArrayList<NameValuePair> 显示 NameValuePair 无法解析

java - comment.charAt(i % comment.length()) 的作用是什么?