2011-06-15 35 views
0

我想解壓縮含有某些PNG圖像的子文件夾歸檔(test.zip):解壓與java中的子文件夾存檔?

test.zip 
    | -> images 
     | -> a.png 
     | -> b.png 

這裏是我做的:

public static void unzip(String archive, File baseFolder, String[] ignoreExtensions) { 
    FileInputStream fin; 
    try { 
     fin = new FileInputStream(archive); 
     ZipInputStream zin = new ZipInputStream(fin); 
     ZipEntry ze = null; 
     while ((ze = zin.getNextEntry()) != null) { 
     if (ignoreExtensions == null || !ignoreEntry(ze, ignoreExtensions)) { 
      File destinationFile = new File(baseFolder, ze.getName()); 
      unpackEntry(destinationFile, zin); 
     } 
     } 
     zin.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 

    private static void unpackEntry(File destinationFile, ZipInputStream zin) { 
    createParentFolder(destinationFile); 
    FileOutputStream fout = null; 
    try { 
     fout = new FileOutputStream(destinationFile); 
     for (int c = zin.read(); c != -1; c = zin.read()) { 
     fout.write(c); 
     zin.closeEntry(); 
     fout.close(); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    } 

    private static void createParentFolder(File destinationFile) { 
    File parent = new File(destinationFile.getParent()); 
    parent.mkdirs(); 
    } 

的圖像被提取到正確的位置但是腐敗(尺寸比預期的小,所以我假設他們沒有解壓縮)。

如果我用7Zip打開test.zip文件,它工作正常。有關如何解壓縮子文件夾的任何想法?

回答

2

你在這裏做什麼?

for (int c = zin.read(); c != -1; c = zin.read()) { 
    fout.write(c); 
    zin.closeEntry(); 
    fout.close(); 
    } 

難道這是你的意思嗎?

for (int c = zin.read(); c != -1; c = zin.read()) { 
    fout.write(c); 
    } 
    zin.closeEntry(); 
    fout.close(); 
+0

謝謝,就是這樣! – u123 2011-06-15 21:49:34

+1

此外,爲了提高效率,您應該更好地使用緩衝陣列,而不是單獨讀寫每個字節。 (你正在寫的每個字節至少有兩個本地調用:一個在Inflater(zlib)中,一個在FileOutputStream中,本地調用是昂貴的。) – 2011-06-15 21:55:54

0

可以通過檢查解壓縮的條目是否是目錄來完成。如果目錄存在,則創建該目錄並繼續在目錄內傳輸文件。

private void unZipFile(long lBatchID, String sFileName) throws Exception { 
    final int BUFFER = 2048; 
    BufferedOutputStream dest = null; 
    FileInputStream fis = null; 
    ZipInputStream zis = null; 
    int iSubstr1 = sFileName.indexOf("-"); 
    int iSubstr2 = sFileName.lastIndexOf("-"); 
    int iEDocketSubStr = sFileName.lastIndexOf("\\"); 
    String sBatchNum = sFileName.substring(iSubstr1 + 1, 
      iSubstr2); 
    String sEDocketNum = sFileName.substring(iEDocketSubStr + 1, 
      iSubstr1); 
    Date startTime = new Date(); 
    try { 

     fis = new FileInputStream(sFileName); 
     zis = new ZipInputStream(
       new BufferedInputStream(fis)); 
     ZipEntry entry; 
     String sTempDir = TEMP_DIR + "\\" + sEDocketNum+"-"+sBatchNum; 
     File fTempDir = new File(sTempDir); 
     fTempDir.mkdirs(); 
     while ((entry = zis.getNextEntry()) != null) { 
      int count; 
      byte data[] = new byte[BUFFER]; 
      if(entry.isDirectory()) 
      { 
       File f2 = new File(TEMP_DIR + "\\" + sEDocketNum+"-"+sBatchNum+"\\"+entry.getName()); 
       f2.mkdir(); 
       logger.debug("Creating directory during unzip....."+entry.getName()); 
      } 
      else 
      { 
      FileOutputStream fos = new FileOutputStream(new File(sTempDir 
        + "\\" + entry.getName())); 
      dest = new BufferedOutputStream(fos, BUFFER); 
      while ((count = zis.read(data, 0, BUFFER)) != -1) { 
       dest.write(data, 0, count); 
      } 
      dest.flush(); 
      dest.close(); 
      } 
     } 
     zis.close(); 
     LogTaskDuration.logDuration(lBatchID, startTime, "UNZIP"); 
    } catch (Exception e) { 

     e.printStackTrace(); 
     logger.error("Problem unzipping file - " + sFileName); 
     throw new Exception(
       "Could not create temporary directory to unzip file"); 
    } 
    finally 
    { 
     if(dest != null) 
      dest.close(); 
     if(fis!=null) 
      fis.close(); 

    } 
}