我试图使用我在网上找到的方法解压缩文件。
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
答案 0 :(得分:1)
destDir.mkdir();
将其更改为:
destDir.mkdirs();
您只创建一个级别的目录。
答案 1 :(得分:0)
确保输出文件夹的名称中没有扩展名,例如" /test.zip"。将其命名为"输出"或者" myFolder"