解压缩文件会在创建目录时复制FileNotFoundException [重复]

问题描述 投票:1回答:1

在此问题被关闭以进行重复之前-是的,我已经阅读了其他帖子,但它们的内容有所不同,答案对我的情况没有帮助。

我的JEE项目中有一个名为Export.zip的zip文件。它位于resources文件夹中。

我正在尝试将其解压缩并将其存储在一个文件夹中,该文件夹将由解压缩器创建。

解压缩器看起来像这样:

public String unzip(String zipFilePath, String destDirectory) throws IOException {
    String path = "";
    File destDir = new File(destDirectory);

    if (!destDir.exists()) {
        destDir.mkdirs();
    }

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

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

    return path;
}

这是引起问题的方法:

public String getPath() throws IOException {
     String destDirectory = "unzip/";
     String pathOuterZip =
     "../standalone/deployments/phonenumbersmanagement-0.0.1-SNAPSHOT.war/WEB-INF/classes/files/";
     this.logger.info("pathOuterZip: " + pathOuterZip);

     //THE LINE BELOW THROWS THE EXCEPTION!

     String filePathInnerZip = unzipper.unzip(pathOuterZip + "Export.zip",
     pathOuterZip + destDirectory);

     //THIS WONT BE EXECUTED CAUSE OF THE EXCEPTION
     this.logger.info("filePathInnerZip: " + filePathInnerZip);
     String pathExportAll = unzipper.unzip(filePathInnerZip, pathOuterZip
     + destDirectory);
     this.logger.info("pathExportAll: " + pathExportAll);

     return pathExportAll;
}

这是我得到的错误:

java.io.FileNotFoundException: ..\standalone\deployments\phonenumbersmanagement-0.0.1-SNAPSHOT.war\WEB-INF\classes\files\unzip\${_DUMMY_DIR}\${_NO_RESTORE}dummy.txt (Das System kann den angegebenen Pfad nicht finden/System cannot find Path) 

问题是,我不知道为什么它指向错误中显示的文本文件。

要了解Export.zip的外观,这是将其解压缩后的结果:

${_DUMMY_DIR} (folder)
${exportWorkingDir} (folder)

第一个文件夹包含${_NO_RESTORE}dummy.txt,在堆栈跟踪中提到。

有人可以帮助我了解发生了什么吗?任何帮助表示赞赏。谢谢。

java jakarta-ee zip unzip
1个回答
0
投票

您正在使用相对路径“ unzip /”;

现在解压缩会在您的WAR文件中查找此文件夹:

.\standalone\deployments\phonenumbersmanagement-0.0.1-SNAPSHOT.war\WEB-INF\classes\files\unzip\${_DUMMY_DIR}\${_NO_RESTORE}dummy.txt

您必须提供文件系统的固定路径。

© www.soinside.com 2019 - 2024. All rights reserved.