无法使用ZipInputStream处理zip文件中的大文件

时间:2013-03-26 11:52:36

标签: java zip

我有一个下面提到的java类,它提取一个zip,并逐个将其内容转换为字符串并打印到控制台。
问题是,当zip内部存在的文件大~80KB时。未显示整个内容(只有3/4的数据转换为字符串并显示在控制台中)。
其次,下面提到的代码在它们之间引入空/空间,如果文件大小小〜1KB

下面提到的代码有什么问题。

 public static void main(String[] args) throws Exception {
    byte[] buf = new byte[1024];
    final int BUFFER = 1024;
    String fName = "c:\\DOC00001.zip";
    ZipInputStream zinstream = new ZipInputStream(
            new FileInputStream(fName));
    ZipEntry zentry = zinstream.getNextEntry();

    while (zentry != null) {            
    byte data[] = new byte[BUFFER];
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    while ((zinstream.read(data, 0, BUFFER)) != -1) {
        out.write(data);
    }       
    InputStream is = new ByteArrayInputStream(out.toByteArray());
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer, "UTF-8");
    String response = writer.toString();
    System.out.println(response);
    zentry = zinstream.getNextEntry();
    }
    zinstream.close();
}

1 个答案:

答案 0 :(得分:0)

read方法无法保证读取完整缓冲区;返回已读取的字节数。从zip文件或任何InputStream中提取数据的正确方法是:

byte[] data = new byte[BUFFER];
ByteArrayOutputStream out = new ByteArrayOutputStream();
int bytesRead;
while ((bytesRead = zinstream.read(data, 0, BUFFER)) != -1) {
    out.write(data, 0, bytesRead);
}

或者,因为您已经在使用IOUtils

ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(zinstream, out);

或者,如果你只是写一个ByteArrayOutputStream来写一个字符串,你可以完全跳过ByteArrayOutputStream

while (zentry != null) {
  StringWriter writer = new StringWriter();
  IOUtils.copy(zinstream, writer, "UTF-8");
  String response = writer.toString();
  System.out.println(response);
  zentry = zinstream.getNextEntry();
}