How to unzip files recursively in Java?

后端 未结 9 2085
慢半拍i
慢半拍i 2020-11-27 03:10

I have zip file which contains some other zip files.

For example, the mail file is abc.zip and it contains xyz.zip, class1.java

9条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 03:37

    Same as NeilMonday's answer, but extracts empty directories:

    static public void extractFolder(String zipFile) throws ZipException, IOException 
    {
        System.out.println(zipFile);
        int BUFFER = 2048;
        File file = new File(zipFile);
    
        ZipFile zip = new ZipFile(file);
        String newPath = zipFile.substring(0, zipFile.length() - 4);
    
        new File(newPath).mkdir();
        Enumeration zipFileEntries = zip.entries();
    
        // Process each entry
        while (zipFileEntries.hasMoreElements())
        {
            // grab a zip file entry
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();
            File destFile = new File(newPath, currentEntry);
            //destFile = new File(newPath, destFile.getName());
            File destinationParent = destFile.getParentFile();
    
            // create the parent directory structure if needed
            destinationParent.mkdirs();
    
            if (!entry.isDirectory())
            {
                BufferedInputStream is = new BufferedInputStream(zip
                .getInputStream(entry));
                int currentByte;
                // establish buffer for writing file
                byte data[] = new byte[BUFFER];
    
                // write the current file to disk
                FileOutputStream fos = new FileOutputStream(destFile);
                BufferedOutputStream dest = new BufferedOutputStream(fos,
                BUFFER);
    
                // read and write until last byte is encountered
                while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                    dest.write(data, 0, currentByte);
                }
                dest.flush();
                dest.close();
                is.close();
            }
            else{
                destFile.mkdirs()
            }
            if (currentEntry.endsWith(".zip"))
            {
                // found a zip file, try to open
                extractFolder(destFile.getAbsolutePath());
            }
        }
    }
    

提交回复
热议问题