Java - 解压缩文件返回 FileNotFoundException

标签 java zip unzip

我正在尝试使用我在网上找到的方法解压缩文件。

    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(不是目录)

我尝试用以下方法更改错误行:

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();

您仅创建一级目录。

关于Java - 解压缩文件返回 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45006077/

相关文章:

java - 如何在 Glide 中向 GenericRequestBuilder 添加交叉淡入淡出动画

Java Swing Repaint 缓慢或无法工作

php - PHP 发送的 ZIP 存档已损坏

android - 在Android中解压unicode文件名错误

java - 为什么 getNextZipEntry 会跳过几个文件的根目录?

android - 如何在 Android 中解压缩受密码保护的文件

java - 使 Spring Quartz Job Scheduler 在用户操作上运行

java - 在服务器刷新数据之前,套接字如何从服务器接收数据?

php - 如何在 CentOS 7 上安装从头编译的 PHP 7.4 的 zip 扩展?

android - retrofit 是否会自动解压缩 GZIP 文件?