如何使用zip4j从受密码保护的Zip文件中删除文件?得到例外

时间:2018-01-18 05:32:30

标签: java zip4j

这是我的代码,但它提供了一个例外。

net.lingala.zip4j.exception.ZipException: cannot delete old zip file
at net.lingala.zip4j.util.ArchiveMaintainer.restoreFileName(ArchiveMaintainer.java:234)
at net.lingala.zip4j.util.ArchiveMaintainer.initRemoveZipFile(ArchiveMaintainer.java:216)
at net.lingala.zip4j.util.ArchiveMaintainer.removeZipFile(ArchiveMaintainer.java:61)
at net.lingala.zip4j.core.ZipFile.removeFile(ZipFile.java:821)
at net.lingala.zip4j.core.ZipFile.removeFile(ZipFile.java:794)
at com.imimobile.workflow.zip.poc.ZipUtility.removeFileFromZipFile(ZipUtility.java:50)
at com.imimobile.workflow.zip.poc.ProcessExc.main(ProcessExc.java:25)



  public boolean removeFileFromZipFile(String zipFilepath, String filepath) {
    try {
        ZipFile zipFile = new ZipFile(zipFilepath);
        zipFile.removeFile(filepath);
    } catch (ZipException ex) {
        ex.printStackTrace();
        return false;
    }
    return true;
}

如果我调用单个方法Like

,则此代码正常工作
  

removeFileFromZipFile(" E:\ POC \ files \ test.zip"," test.html")

当我尝试按顺序执行以下操作时,它无法正常工作。

  • 从zip中读取文件
  • 从zip
  • 中删除文件
  

getFileFromZip(" E:\ POC \ files \ test.zip"," test.html")   removeFileFromZipFile(" E:\ POC \ files \ test.zip"," test.html")//这里我是   发生异常

 public String getFileFromZip(String zipPath, String filePath) {
    String data = null;
    ZipInputStream is = null;
    try {
        // Initiate the ZipFile
        ZipFile zipFile = new ZipFile(zipPath);
        if (zipFile.isEncrypted()) {
            zipFile.setPassword("abc@123");
        }

        FileHeader fileHeader = zipFile.getFileHeader(filePath);

        if (fileHeader != null) {
            is = zipFile.getInputStream(fileHeader);
            data = IOUtils.toString(is, StandardCharsets.UTF_8);
            System.out.println("Data :: " + data);
        } else {
            System.err.println("FileHeader does not exist");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return data;
}

2 个答案:

答案 0 :(得分:0)

确保在创建文件后关闭该文件,并在尝试删除成员之前重新打开该文件。否则,文件将保持锁定状态,并且在成员删除操作期间无法删除和重新创建。

答案 1 :(得分:0)

我得到了答案,这种方法在进一步使用之前错过了关闭流程。

public String getFileFromZip(String zipPath, String filePath) {
    String data = null;
    ZipInputStream is = null;
    ZipFile zipFile = null;
    try {
        // Initiate the ZipFile
        zipFile = new ZipFile(zipPath);
        if (zipFile.isEncrypted()) {
            zipFile.setPassword("abc@123");
        }

        FileHeader fileHeader = zipFile.getFileHeader(filePath);
        if (fileHeader != null) {
            is = zipFile.getInputStream(fileHeader);
            data = IOUtils.toString(is, StandardCharsets.UTF_8);
            System.out.println("Data :: " + data);
        } else {
            System.err.println("FileHeader does not exist");
        }
    } catch (Exception e) {
        System.err.println(getStringFromStackTrace(e));
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (Exception e) {
        }
    }
    return data;
}