我有一个下面提到的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();
}
答案 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();
}