0

This is the UnzipUtility class used for extracting a zip file, the problem I'm facing is that when I extract the zip file I still have the unzipped folder in with its content, I would need to extract it skipping that level and have the content of it straight away.

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);
        } else {
            // if the entry is a directory, make the directory
            File dir = new File(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 {
    if(Files.notExists(new File(filePath).getParentFile().toPath())) {
        new File(filePath).getParentFile().mkdir();
    }
    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();
}

}

MELS
  • 57
  • 1
  • 7
  • So let's say I have a folder "a" containing 2 text files ("b.txt" and "c.txt"), I zip that folder to be "a.zip", then I would need to extract the content of that zip folder (the 2 text files) to the desired directory, but skipping the "a" folder. – MELS Apr 20 '20 at 19:02
  • Maybe a third parameter in the function specifying what directory in the zip is the root of the extraction? – MELS Apr 20 '20 at 19:07

1 Answers1

0

I implemented a solution for the issue by just adding a third parameter to specify the directory in the zip where the root of the extraction should be, this way we extract the content of the zip file without its parent folder.

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)
 */
public void unzip(String zipFilePath, String destDirectory, String rootLevelDir) throws IOException {

    if(rootLevelDir == null || rootLevelDir.isEmpty()){
        rootLevelDir="/";
    }
    File destDir = new File(destDirectory);
    if (!destDir.exists()) {
        destDir.mkdirs();
    }
    try(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 ( rootLevelDir.equals("/") || entry.getName().startsWith(rootLevelDir)) {

                if (!rootLevelDir.equals("/")) {
                    filePath = filePath.replaceFirst(rootLevelDir, "");
                }
                if (!entry.isDirectory()) {
                    // if the entry is a file, extracts it
                    extractFile(zipIn, filePath);
                } else {
                    // if the entry is a directory, make the directory
                    File dir = new File(filePath);
                    dir.mkdirs();
                }
            }
            zipIn.closeEntry();
            entry = zipIn.getNextEntry();
        }
    }
}

/**
 * Extracts a zip entry (file entry)
 */
private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
    if (Files.notExists(new File(filePath).getParentFile().toPath())) {
        new File(filePath).getParentFile().mkdirs();
    }
    try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) {
        byte[] bytesIn = new byte[BUFFER_SIZE];
        int read;
        while ((read = zipIn.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }
    }
}

}

MELS
  • 57
  • 1
  • 7