2014-01-07 110 views
0

我有一個來自不同位置的文件列表。我使用下面的代碼創建一個zip文件,該代碼無錯誤地工作。但是,當我嘗試解壓文件在Windows中使用全部提取它看不見任何字節,但如果我用Windows資源管理器雙擊進入zip文件本身,我可以看到文件和單獨的文件可以打開幷包含正確的數據無法解壓縮用java創建的zip文件

 ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile)); 
     for (File next : files) 
     { 
      ZipEntry zipEntry = new ZipEntry(next.getName()); 
      zos.putNextEntry(zipEntry); 
      FileInputStream in = new FileInputStream(next); 
      byte[] buf = new byte[1024]; 
      int len; 
      while ((len = in.read(buf)) > 0) 
      { 
       zos.write(buf, 0, len); 
      } 
      zos.closeEntry(); 
      in.close(); 
     } 
     zos.close(); 

回答

-1

這可能是也可能不是相關的,但我發現使用固定字節長度會導致新行字符丟失。

這可能會幫助:

final byte[] newLine = System.getProperty(
     "line.separator").getBytes("UTF-8"); 

while ((line = in.readLine()) != null) 
     final byte[] buffer = line.getBytes("UTF-8"); 
     out.write(buffer, 0, buffer.length); 
     out.write(newLine, 0, newLine.length); 
}