我創建一個程序,將提取一個zip,然後將文件插入到數據庫中,每隔一段時間,我得到了錯誤java的提取物拉鍊ZLIB輸入流意外結束
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'可以解壓縮它)? •請確定實際產生異常的代碼行。 –
我可能記得沒有正確,但是你不應該在處理它後關閉每個條目嗎? – biziclop
您應該在調用insertZipEntry()之前關閉baos,並且我們的'currentByte'變量名稱很少:應該是'count'或類似的。 – EJP