【发布时间】:2022-02-01 17:15:26
【问题描述】:
我正在创建一个程序,它会提取一个 zip,然后将文件插入数据库,但我经常会收到错误
java.lang.Exception: java.io.EOFException: Unexpected end of ZLIB input stream
我无法确定原因,因为提取代码与您可以在网络上找到的所有其他代码几乎相同。我的代码如下:
public void extract(String zipName, InputStream content) throws Exception {
int BUFFER = 2048;
//create the zipinputstream
ZipInputStream zis = new ZipInputStream(content);
//Get the name of the zip
String containerName = zipName;
//container for the zip entry
ZipEntry entry;
// Process each entry
while ((entry = zis.getNextEntry()) != null) {
//get the entry file name
String currentEntry = entry.getName();
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// establish buffer for writing file
byte data[] = new byte[BUFFER];
int currentByte;
// read and write until last byte is encountered
while ((currentByte = zis.read(data, 0, BUFFER)) != -1) {
baos.write(data, 0, currentByte);
}
baos.flush(); //flush the buffer
//this method inserts the file into the database
insertZipEntry(baos.toByteArray());
baos.close();
}
catch (Exception e) {
System.out.println("ERROR WITHIN ZIP " + containerName);
}
}
}
【问题讨论】:
-
• 您是否检查过您尝试处理的 ZIP 流(文件)是否有效(例如,
unzip可以解压)? • 请查明代码中实际引发异常的行。 -
我可能记错了,但您不应该在处理完每个条目后关闭它吗?
-
你应该在调用insertZipEntry()之前关闭包,我们的'currentByte'变量命名不当:应该是'count'或类似的。
-
您能为您的问题选择一个最佳答案吗?
标签: java