2016-11-15 132 views
0

我收到zip擴展名中的壓縮文件。 我無法使用Windows資源管理器直接打開它。 我可以使用7Zip提取它,它會拋出一些錯誤,但文件仍然按預期解壓縮。 我可以使用winrar解壓縮它,沒有錯誤,按預期解壓縮文件。嘗試使用java.util.zip/zip4j解壓縮時出現空文件夾

然後我試着用java.util.unzip/zip4j解壓縮。

java.util.zip代碼:

public static void unzip(String zipFilePath, 
         String destDirectory) throws IOException { 
    File destDir = new File(destDirectory); 
    if (!destDir.exists()) { 
     destDir.mkdir(); 
    } 
    ZipInputStream zipIn = 
     new ZipInputStream(new FileInputStream(zipFilePath)); 
    ZipEntry entry = zipIn.getNextEntry(); 
    // iterates over entries in the zip file 
    while (entry != null) { 
     String filePath = destDirectory + File.separator + entry.getName(); 
     if (!entry.isDirectory()) { 
      // if the entry is a file, extracts it 
      extractFile(zipIn, filePath); 
     } else { 
      // if the entry is a directory, make the directory 
      File dir = new File(filePath); 
      dir.mkdir(); 
     } 
     zipIn.closeEntry(); 
     entry = zipIn.getNextEntry(); 
    } 
    zipIn.close(); 
} 

/** 
* Extracts a zip entry (file entry) 
* @param zipIn 
* @param filePath 
* @throws IOException 
*/ 
private static void extractFile(ZipInputStream zipIn, 
           String filePath) throws IOException { 
    BufferedOutputStream bos = 
     new BufferedOutputStream(new FileOutputStream(filePath)); 
    byte[] bytesIn = new byte[BUFFER_SIZE]; 
    int read = 0; 
    while ((read = zipIn.read(bytesIn)) != -1) { 
     bos.write(bytesIn, 0, read); 
    } 
    bos.close(); 
} 

與上面的代碼,沒有錯誤發生,但我得到一個空文件夾。

zip4j代碼:

 String zipFilePath = "C:\\Juw\\JR\\file\\output\\020030214112016.zip"; 
    //String zipFilePath = "C:\\Juw\\JR\\file\\output\\myFile.zip"; 
    String destDirectory = "C:\\Juw\\JR\\file\\output\\targetUnzip"; 

    try { 
     ZipFile zipFile = new ZipFile(zipFilePath); 
     zipFile.extractAll(destDirectory); 

    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 

而且有例外: net.lingala.zip4j.exception.ZipException:郵編找不到頭。可能不是一個zip文件

如果我試圖解壓縮使用winrar的文件,然後我使用壓縮功能內置的Windows壓縮它。我可以使用我的代碼成功解壓縮它。而我和客戶給我的大小也不一樣。我的是508kb,另一個是649kb。

問題是: - 有沒有任何Java庫,利用/作爲強大的winrar,可以提取壓縮文件沒有錯誤? - 解決此案例的最佳做法是什麼?

非常感謝提前:)

+3

「什麼是最好的做法來解決這種情況?」 - 要求客戶提供有效的.zip文件。 – JimmyB

+0

嗨JimmyB,你懷疑zip文件無效嗎?因爲winrar可以提取它沒有任何問題。客戶端有可能使用具有不同標準zip的第三方應用程序壓縮它?或從其他操作系統(而不是Windows)? – cumibulat

+0

如果Windows和7zip以及Java lib失敗或報告錯誤,我認爲該文件在某些​​方面可能不完全有效。也許它甚至不是一個zip文件,而是一些其他格式寫入名爲.zip的文件。不依賴文件名來確定文件實際具有的文件的工具可能仍然能夠解壓縮它。 – JimmyB

回答

0

感謝所有幫助我的受訪者。

因此,這個gzip文件(名爲.zip擴展名)在文件末尾有一個空字符串。所以它像我在之前的文章中提到的那樣拋出錯誤。用我從同事那裏得到的代碼,問題就解決了。我發佈它,所以也許其他用戶將來可以使用它。

public void gunzipIt(){ 

byte[] buffer = new byte[1024]; 
boolean isValid = true; 

try{ 

    GZIPInputStream gzis = 
     new GZIPInputStream(new FileInputStream(INPUT_GZIP_FILE)); 

    FileOutputStream out = 
     new FileOutputStream(OUTPUT_FILE); 

    while (isValid) { 

     int len; 

     try{ 
      len = gzis.read(buffer); 
     }catch(Exception ex){ 
      len = 0; 
      isValid = false; 
     } 

     if (len > 0) { 
      out.write(buffer, 0, len); 
     }else{ 

      isValid = false; 
     } 
    } 

    gzis.close(); 
    out.close(); 

    System.out.println("Done"); 

}catch(IOException ex){ 
    ex.printStackTrace(); 
} 

}

相關問題