java - 解压 zip 文件时出错

标签 java

我正在尝试使用 java.util.zip 包解压缩压缩文件夹:

现在我的压缩文件夹结构是: 我的压缩文件夹名称是classes.zip 在这个 zip 文件夹中,我有一个 classes 文件夹,里面有子文件夹和文件: enter image description here

如果你进一步进入 www 文件夹,那么它又有一个子文件夹,它是一个 java 包,在包结构文件夹中我有 .class 文件。

现在我正在尝试解压缩这个压缩文件夹,我的代码是: 包 www.eor.com;

/**

* A console application that tests the UnzipUtility class
 *
 */
public class UnzipUtilityTest {
    public static void main(String[] args) {
        String zipFilePath = "D:/classes.zip";
        String destDirectory = "D:/Dojo";
        UnzipUtility unzipper = new UnzipUtility();
        try {
            unzipper.unzip(zipFilePath, destDirectory);
        } catch (Exception ex) {
            // some errors occurred
            ex.printStackTrace();
        }
    }
}

支持类是:

package www.eor.com;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * This utility extracts files and directories of a standard zip file to
 * a destination directory.
 */
public class UnzipUtility {
    /**
     * Size of the buffer to read/write data
     */
    private static final int BUFFER_SIZE = 4096;
    /**
     * Extracts a zip file specified by the zipFilePath to a directory specified by
     * destDirectory (will be created if does not exists)
     * @param zipFilePath
     * @param destDirectory
     * @throws IOException
     */
    public void unzip(String zipFilePath, String destDirectory) throws IOException {
        File destDir = new File(destDirectory);
        if (!destDir.exists()) {
            destDir.mkdir();
        }
        ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
        ZipEntry entry = zipIn.getNextEntry();
        // iterates over entries in the zip file
        while (entry != null) {
            String filePath = destDirectory + File.separator + entry.getName();
            if (!entry.isDirectory()) {
                // if the entry is a file, extracts it
                extractFile(zipIn, filePath);
            } else {
                // if the entry is a directory, make the directory
                File dir = new File(filePath);
                dir.mkdir();
            }
            zipIn.closeEntry();
            entry = zipIn.getNextEntry();
        }
        zipIn.close();
    }
    /**
     * Extracts a zip entry (file entry)
     * @param zipIn
     * @param filePath
     * @throws IOException
     */
    private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] bytesIn = new byte[BUFFER_SIZE];
        int read = 0;
        while ((read = zipIn.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }
        bos.close();
    }
}

现在,当我运行 UnzipUtilityTest 类时,出现异常:

java.io.FileNotFoundException: D:\Dojo\classes\camel-config-xml.xml (The system cannot find the path specified)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:110)
    at www.cognizant.com.UnzipUtility.extractFile(UnzipUtility.java:59)
    at www.cognizant.com.UnzipUtility.unzip(UnzipUtility.java:41)
    at www.cognizant.com.UnzipUtilityTest.main(UnzipUtilityTest.java:16)

为什么会给出这个异常以及如何纠正这个问题?

最佳答案

可能是因为文件classes/的父文件不存在,所以无法在其中创建文件。

当您提取 zip 的条目时,您必须为该文件创建 文件夹。 zip 不一定包含每个文件夹的条目。但是 zip 中的每个条目都是 path/to/entry/filename.ext 的形式,因此您可以导出条目的父路径并相应地创建父文件夹。

所以在提取文件之前,做

new File(filePath).getParent().mkdirs()

关于java - 解压 zip 文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37203402/

相关文章:

java - 同步块(synchronized block)中需要一个对象

java - UnsatisfiedLinkError using JNI for Native C Package libfprint

java - 细进度条

文件上的 java 操作 - java.io.File 或 cmd 命令

java - 使用 JDK 6 中捆绑的 jax-rs 引用实现构建 Web 服务

java - 如何解决 java.lang.ClassNotFoundException : org. gradle.wrapper.GradleWrapperMain 错误?

java - instanceof 与多态性

java - 如何在没有任何 for 循环的情况下递归地解压缩字符串?

java - 在一个特定页面上给出 "java.net.SocketTimeoutException: Read timed out"

java - 在 HTML 表单中发送 API key