2015-12-09 61 views
0

我嘗試了所有可能的方法,並且搜索了很多。我找到的唯一解決方案是Adding (and overriding) files to compressed archive,但是當我在zipFile.renameTo(tempFile)行中運行代碼時,發生了一些事情,它只是在那裏掛起。我無法弄清楚發生了什麼?Java從現有ZipFile中刪除條目

任何人都可以幫助我嗎?

 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

拋出RuntimeException,而不是「e.printStackTrace( );」發現問題要容易得多。 – andrucz

+0

問題是重新命名爲tempfile需要很長時間。有沒有什麼可能的方法來加速它 – ARxAR

回答

1

你應該把

tempFile.delete(); 

這條線後重命名zip文件

boolean renameOk=zipFile.renameTo(tempFile); 

,所以它像

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