2012-01-10 131 views
1

我工作的應用程序,它從一個壓縮文件需要,並把它們在其他,它的罰款文件,但如果沒有在源目錄ZIP它失敗,出現以下異常:爪哇 - ZIP輸出流動態緩衝區大小

Exception in thread "main" java.util.zip.ZipException: invalid entry size (expected 1374 but got 1024 bytes) 

我用下面的代碼:

public static void ZipExtractToZip(File inZip, File outZip) throws IOException 
{ 
    ZipInputStream zis = new ZipInputStream(new FileInputStream(inZip)); 
    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outZip))); 
    byte[] buffer = new byte[1024]; 

    for (ZipEntry ze = zis.getNextEntry(); ze != null; ze = zis.getNextEntry()) 
    { 
     zos.putNextEntry(ze); 
     for (int read = zis.read(buffer); read != -1; read = zis.read(buffer)) { 
      zos.write(buffer, 0, read); 
     } 
     zos.closeEntry(); 
    } 

    zos.close(); 
    zis.close(); 
} 

我已經嘗試了不同的緩衝區大小,但這樣做沒有幫助,我需要一種方式來獲得一個動態的緩衝區大小。 歡迎舉例和鏈接。

編輯:我改變了代碼,使其可用

+0

哪條線有例外?順便說一句:我也會使用BufferedInputSTream(不是它可以解決問題) – 2012-01-10 10:49:44

+0

at java.util.zip.ZipOutputStream.closeEntry(Unknown Source) \t at com.hachisoftware.mmi.system.Util.ZipExtractToZip(Util。 java:26) – 2012-01-10 10:53:09

回答

2

移動

zos.closeEntry(); 

最內層循環外,否則我們假定你是每個條目不超過1024個字節長。

我猜你的目錄是第一個這樣大小的條目。


順便說一句,您還可以在外環之前移動

byte[] buffer = new byte[1024]; 

如此它僅創建一次。

+0

我會嘗試一下 – 2012-01-10 10:59:28

+0

謝謝,這個工作,我得到了一點壓縮(1kb),但多數民衆贊成 – 2012-01-10 11:01:32

+0

此外,如果你這樣做,你只需要創建一次,在外部循環外的緩衝區。 – sje397 2012-01-10 11:03:07