使用 zip4j Java 解压 .zip 文件

Decompress .zip file with Java using zip4j

我正在尝试使用来自 net.lingala.zip4j

的 API ZipFile 解压一个 zip 文件
public static void unzip(File zipf, File baseDir) throws IOException, ZipException {

    String source = zipf.getAbsolutePath();//"some/compressed/file.zip";
    String destination = baseDir.getPath();//"some/destination/folder";
  //  String password = "password";

    try {
         ZipFile zipFile = new ZipFile(source);
         if (zipFile.isEncrypted()) {
           // zipFile.setPassword(password);
         }
         zipFile.extractAll(destination);
    } catch (ZipException e) {
        e.printStackTrace();
    }
}

我总是遇到错误:

net.lingala.zip4j.exception.ZipException: Probably not a zip file or a corrupted zip file

是否有其他解决方案可以正确解压 file.zip?

你的代码很完美。我的系统上有 运行 你的代码,它运行得非常好。我使用了 http://central.maven.org/maven2/net/lingala/zip4j/zip4j/1.3.1/zip4j-1.3.1.jar 中的 zip4j-1.3.1.jar。请使用不同的 zip 文件再次 运行。您的 zip 文件可能已损坏。