有没有办法解压缩包含各种子文件夹的文件?

Is there a way to unzip a file with various subfolders?

有没有一种方法可以在 Java 中解压缩包含各种子文件夹的 ZIP 文件而不需要安装任何额外的插件。

例如,我想使用以下代码解压缩文件 https://services.gradle.org/distributions/gradle-6.4.1-bin.zip

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class unzipfile {

    private static void unzip(String zipFilePath, String zipFileName, String destDir) {
        System.out.print("Extracting " + zipFileName + "...");
        File dir = new File(destDir);
        // create output directory if it doesn't exist
        if(!dir.exists()) dir.mkdirs();
        FileInputStream fis;
        //buffer for read and write data to file
        byte[] buffer = new byte[1024];
        try {
            fis = new FileInputStream(zipFilePath);
            try (ZipInputStream zis = new ZipInputStream(fis)) {
                ZipEntry ze = zis.getNextEntry();
                while(ze != null){
                    String fileName = ze.getName();
                    File newFile = new File(destDir + File.separator + fileName);
                    //create directories for sub directories in zip
                    new File(newFile.getParent()).mkdirs();
                    try (FileOutputStream fos = new FileOutputStream(newFile)) {
                        int len;
                        while ((len = zis.read(buffer)) > 0) {
                            fos.write(buffer, 0, len);
                        }
                    }
                    //close this ZipEntry
                    ze = zis.getNextEntry();
                }
            }
            fis.close();
            System.out.println(" Done!");
        } catch (IOException e) {
            System.out.println(" Error while extracting " + zipFileName);
            Logger.getLogger(unzipfile.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    public static void main(String[] args) {
        try{
            unzip(args[0], args[1], args[2]);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("No valid arguments specified. Usage:\n");
            System.out.println("\'java unzipfile [ZIP file path with file name] [file name only] [target directory]\'");
            System.exit(1);
        }
    }

}

我运行:

java unzipfile path\to\file\gradle-6.4.1-bin.zip gradle-6.4.1-bin.zip .

但是当我 运行 完整的应用程序时,它给我消息:

Extracting gradle-6.4.1-bin.zip... Error while extracting gradle-6.4.1-bin.zip
may. 27, 2020 5:44:37 P.áM. unzipfile unzip
SEVERE: null
java.io.FileNotFoundException: .\gradle-6.4.1\README (El sistema no puede encontrar la ruta especificada)
        at java.base/java.io.FileOutputStream.open0(Native Method)
        at java.base/java.io.FileOutputStream.open(FileOutputStream.java:291)
        at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:234)
        at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:184)
        at unzipfile.unzip(unzipfile.java:29)
        at unzipfile.main(unzipfile.java:50)

感谢您的宝贵时间。

最后,我找到了这个工作代码:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class unzipfile {
    public static void main(String[] args) {
        String filename = args[0];
        File srcFile = new File(filename);
        // create a directory with the same name to which the contents will be extracted
        String zipPath = filename.substring(0, filename.length()-4);
        File temp = new File(zipPath);
        temp.mkdir();
        ZipFile zipFile = null;
        try { 
            zipFile = new ZipFile(srcFile);
            // get an enumeration of the ZIP file entries
            Enumeration e = zipFile.entries();
            while (e.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) e.nextElement();
                File destinationPath = new File(zipPath, entry.getName());
                //create parent directories
                destinationPath.getParentFile().mkdirs();
                // if the entry is a file extract it
                if (!entry.isDirectory())                     
                System.out.println("Extracting file: " + destinationPath);

                try (BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry))) {
                    int b;
                    byte buffer[] = new byte[1024];

                    FileOutputStream fos = new FileOutputStream(destinationPath);

                    try (BufferedOutputStream bos = new BufferedOutputStream(fos, 1024)) {
                        while ((b = bis.read(buffer, 0, 1024)) != -1) {
                            bos.write(buffer, 0, b);
                        }
                    }
                }
            }
        }
        catch (IOException ioe) {
            System.out.println("Error opening zip file" + ioe);
        }
         finally {
             try {
                 if (zipFile!=null) {
                     zipFile.close();
                 }
             }
             catch (IOException ioe) {
                    System.out.println("Error while closing zip file" + ioe);
             }
         } 
    }
}