2012-10-29 94 views
7

我試圖解壓文件(從FTP服務器檢索):解鏈文件ZIP異常:無效的條目大小(預期193144卻得到了193138個字節)

ZipInputStream zis = new ZipInputStream(
    new FileInputStream(zipFile)); 
    ZipEntry ze = zis.getNextEntry(); 
    while (ze != null) { 
     String fileName = ze.getName(); 
     File newFile = new File(outputFileName+outputFolder + File.separator + fileName); 
     System.out.println("file unzip : " + newFile.getAbsoluteFile()); 
     FileOutputStream fos = new FileOutputStream(newFile); 
     int len; 
     while ((len = zis.read(buffer)) > 0) { 
      fos.write(buffer, 0, len); 
     } 
     fos.close(); 
     sendFile = newFile; 
     ze = zis.getNextEntry(); 
    } 
    zis.closeEntry(); 
    zis.close(); 
    System.out.println("Done"); 

我在只有一個文本文件。 zip文件。這段代碼可以在我的本地windows機器上正常工作。然而,當部署到Ubuntu的服務器,它拋出以下異常..

java.util.zip.ZipException: invalid entry size (expected 193144 but got 193138 bytes) 
at java.util.zip.ZipInputStream.readEnd(ZipInputStream.java:386) 
at java.util.zip.ZipInputStream.read(ZipInputStream.java:156) 
at java.io.FilterInputStream.read(FilterInputStream.java:90) 

在com.empress.Xsync.updater.ClientConfiguration.unZipFile(ClientConfiguration.java:246)

我已經手動解壓縮它..工作正常。 原始.txt文件大小爲193144字節。

+0

它可能與問題無關,但是'InputStream.read(...)'在EOF處返回'-1'並且可以返回0 '沒有錯誤,所以你應該檢查'> = 0'或'!= -1',而不是'> 0'。 –

+0

也試過了..沒有解決問題! – simpleJack

回答

9

看起來您的zip文件在傳輸到Ubuntu機器的過程中已損壞。嘗試從Ubuntu機器上的命令行解壓縮相同的文件,以查看它是否也報告問題。

如果我想做一個隨機猜測,那就是你通過FTP傳輸ZIP文件並使用'ascii'模式而不是'binary'模式。 (FTP可能已經將'\r\n'轉換爲'\n'六次...)

+0

紅心!非常謝謝Stephan ..那幫了很多.. – simpleJack

相關問題