2014-01-07 113 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); 
}