![](/img/trans.png)
[英]Invalid entry size (ZipException) when reading Excel file in java
[英]ZipException when unzipping file in Java
我使用Java處理zip文件時非常新鮮,我遇到了一個奇怪的情況。
這是我用於解壓縮的方法:
public void unzip(File zipFile, File rootDir) throws IOException
{
ZipFile zip = new ZipFile(zipFile);
Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();
while(entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
java.io.File f = new java.io.File(rootDir, entry.getName());
if (entry.isDirectory()) { // if its a directory, create it
continue;
}
if (!f.exists()) {
f.getParentFile().mkdirs();
f.createNewFile();
}
/*BufferedInputStream bis = new BufferedInputStream(zip.getInputStream(entry)); // get the input stream
BufferedOutputStream bos = new BufferedOutputStream(new java.io.FileOutputStream(f));
while (bis.available() > 0) { // write contents of 'is' to 'fos'
bos.write(bis.read());
}
bos.close();
bis.close();*/
InputStream is = zip.getInputStream(entry);
OutputStream os = new java.io.FileOutputStream(f);
byte[] buf = new byte[4096];
int r ;
while ((r = is.read(buf)) != -1) {
os.write(buf, 0, r);
}
os.close();
is.close();
}
}
但是,拋出了IOException並且消息是:
信息| jvm 1 | 2012/11/30 01:58:05 | java.util.zip.ZipException:打開zip文件時出錯
信息| jvm 1 | 2012/11/30 01:58:05 | at java.util.zip.ZipFile.open(Native Method)
信息| jvm 1 | 2012/11/30 01:58:05 | 在java.util.zip.ZipFile。(ZipFile.java:127)
信息| jvm 1 | 2012/11/30 01:58:05 | 在java.util.zip.ZipFile。(ZipFile.java:143)
有人可以幫我嗎?
非常感謝。
更新:
我使用Linux作為測試環境。 解壓縮目錄的權限是drwxr-xr-x -
更新02:
通過采用@heikkim的建議,
我剛嘗試在linux中使用unzip commend,試圖手動解壓縮我的文件。 我有以下消息:
存檔:TMA_Template.zip警告:zipfile注釋截斷警告[TMA_Template.zip]:zipfile聲稱是多部分存檔的最后一個磁盤; 無論如何都要嘗試處理,假設所有部件按順序連接在一起。 期待“錯誤”和警告......真正的多部分支持尚不存在(即將推出)。 錯誤[TMA_Template.zip]:在zipfile中缺少6366880279個字節(無論如何都要處理)錯誤[TMA_Template.zip]:嘗試在開始壓縮文件之前進行搜索(請檢查您是否已在相應的BINARY模式下傳輸或創建了zipfile,並且你已經正確編譯了UnZip)
你可以嘗試這種方法:
private void unzip() throws IOException {
int BUFFER = 2048;
BufferedOutputStream dest = null;
BufferedInputStream is = null;
ZipEntry entry;
ZipFile zipfile = new ZipFile("latest.zip");
Enumeration e = zipfile.entries();
(new File(root)).mkdir();
while (e.hasMoreElements()) {
entry = (ZipEntry) e.nextElement();
//outText.setText(outText.getText() + "\nExtracting: " + entry);
if (entry.isDirectory()) {
(new File(root + entry.getName())).mkdir();
} else {
(new File(root + entry.getName())).createNewFile();
is = new BufferedInputStream(zipfile.getInputStream(entry));
int count;
byte data[] = new byte[BUFFER];
FileOutputStream fos = new FileOutputStream(root + entry.getName());
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = is.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
is.close();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.