Java从现有ZipFile中删除条目

时间:2015-12-09 11:35:19

标签: 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();

            }

1 个答案:

答案 0 :(得分:1)

你应该放置

boolean renameOk=zipFile.renameTo(tempFile);

重命名zipFile之后的这一行

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

所以它就像

var dataString='[{"Company":"ABC","Focus":"Operate","Completed":"50","Remaining":"25"},{"Company":"ABC","Focus":"Operate","Completed":"20","Remaining":"50"},{"Company":"DEF","Focus":"Optimize","Completed":"100","Remaining":"75"},{"Company":"XYZ","Focus":"Innovate","Completed":"100","Remaining":"75"},{"Company":"ABC","Focus":"Improve","Completed":"50","Remaining":"50"},{"Company":"ABC","Focus":"Optimize","Completed":"50","Remaining":"25"},{"Company":"DEF","Focus":"Operate","Completed":"50","Remaining":"25"},{"Company":"ABC","Focus":"Improve","Completed":"50","Remaining":"25"},{"Company":"XYZ","Focus":"Operate","Completed":"50","Remaining":"25"},{"Company":"XYZ","Focus":"Operate","Completed":"50","Remaining":"25"},{"Company":"XYZ","Focus":"Optimize","Completed":"20","Remaining":"50"}]';