简体   繁体   中英

Java: Unzip Folder with subfolders

I am having issue with extracting files into subfolders. The zip files I need to unzip have multiple folders, and I am trying to keep the directory structure when unzipping.

package components;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * This utility extracts files and directories of a standard zip file to
 * a destination directory.
 * 
 *
 */
public class UnzipUtility {
    /**
     * Size of the buffer to read/write data
     */
    private static final int BUFFER_SIZE = 4096;
    /**
     * Extracts a zip file specified by the zipFilePath to a directory specified by
     * destDirectory (will be created if does not exists)
     * @param zipFilePath
     * @param destDirectory
     * @throws IOException
     */
    public void unzip(String zipFilePath, String destDirectory) throws IOException {
        File destDir = new File(destDirectory);
        if (!destDir.exists()) {
            destDir.mkdir();
        }
        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);
                System.out.println(filePath);
            } else {
                // if the entry is a directory, make the directory
                File dir = new File(filePath);
                System.out.println(filePath);
                dir.mkdir();
            }
            zipIn.closeEntry();
            entry = zipIn.getNextEntry();
        }
        zipIn.close();
    }
    /**
     * Extracts a zip entry (file entry)
     * @param zipIn
     * @param filePath
     * @throws IOException
     */
    private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] bytesIn = new byte[BUFFER_SIZE];
        int read = 0;
        while ((read = zipIn.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }
        bos.close();
    }
}

The output I get from the code is below. The issue is that it the file DoD-DISA-logos-as-JPEG.jpg is part of a child folder to subfolder. So path should be folder/subfolder/childfolder/DoD-DISA-logos-as-JPEG.jpg. It is not including that childfolder so it gets IO exception I believe.

java.io.FileNotFoundException: /Users/user/Desktop/folder/subfolder/DoD-DISA-logos-as-JPEG.jpg (No such file or directory)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(FileOutputStream.java:270)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
    at components.UnzipUtility.extractFile(UnzipUtility.java:62)
    at components.UnzipUtility.unzip(UnzipUtility.java:42)
    at components.UnzipUtilityTest.main(UnzipUtilityTest.java:9)

You should not rely on entry.isDirectory() or on the fact that the ZipEntry iterator will list all the folders hierarchies.

A zip files structure may contain only a ZipEntry file wich contains in its name a subfolders hierarchy. Eg. foo/bar/file.txt will be a ZipEntry but not foo/ nor foo/bar/

Thus for :

  • a file, you should get the parent directory and create the whole hierarchy if it does not exists with File#mkdirs
  • a directory, you should create the whole hierarchy if it does not exists with File#mkdirs

Your code should be transformed to something like that:

       if (!entry.isDirectory()) {
            // if the entry is a file, extracts it
            new File(filePath).getParentFile().mkdirs();
            extractFile(zipIn, filePath);
            System.out.println(filePath);
        } else {
            // if the entry is a directory, make the directory
            File dir = new File(filePath);
            System.out.println(filePath);
            dir.mkdirs();
        }

I recommend to use an external library to zip/unzip like zt-zip .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM