Java - 正在解压缩文件 returns FileNotFoundException

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”。将其命名为 "output" 或 "myFolder"