![](/img/trans.png)
[英]Getting FileNotFoundException while unzipping a zip file though Java
[英]Java - Unzipping file returns FileNotFoundException
我正在嘗試使用在網上找到的方法來解壓縮文件。
public static void unzipFile(String zipFile, String outputFolder) throws IOException {
File destDir = new File(outputFolder);
if (!destDir.exists()) {
destDir.mkdir();
}
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFile));
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
String filePath = outputFolder + File.separator + entry.getName();
if (!entry.isDirectory()) {
extractFile(zipIn, filePath);
} else {
File dir = new File(filePath);
dir.mkdir();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
}
public static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
byte[] bytesIn = new byte[4096];
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
bos.close();
}
但是,我繼續獲取FileNotFoundException BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
錯誤消息: java.io.FileNotFoundException: /Users/michael/NetBeansProjects/test/build/web/TEST_ZIP/my-html/css/bootstrap-theme.css (Not a directory)
我試圖用以下方式更改錯誤行:
File file = new File(filePath);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
但是也沒有用。 控制台中顯示相同的錯誤消息。
我的ZIP文件結構:
my-html
|
|- css
| |
| |- bootstrap-theme.css
| |- ..
| |- ..
|
|-index.html
destDir.mkdir();
更改為:
destDir.mkdirs();
您僅創建一級目錄。
確保輸出文件夾的名稱沒有擴展名,例如“ /test.zip”。 將其命名為“輸出”或“ myFolder”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.