Extract .zip File in Java using Apache Commons Compress

Tags: Apache Commons Apache Commons Compress ZipArchiveEntry ArchiveException ArchiveInputStream ArchiveStreamFactory IOUtils File Java IO Java NIO

In this Java tutorial, we learn how to use the Apache Commons Compress library to extract a .zip file into a directory in the Java program.

Add Apache Commons Compress library to your Java project

To use Apache Commons Compress Java library in the Gradle build project, add the following dependency into the build.gradle file.

compile group: 'org.apache.commons', name: 'commons-compress', version: '1.20'

To use Apache Commons Compress Java library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.20</version>
</dependency>

To download the Apache Commons Compress jar file you can visit Apache Commons Compress download page at commons.apache.org

Implement ZipFileCompressUtils class

First step, we implement a new class named ZipFileCompressUtils and introduce extractZip() public method to extract a .zip file into a directory.

ZipFileCompressUtils.java

import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.utils.IOUtils;

import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ZipFileCompressUtils {

    public void extractZip(String zipFilePath, String extractDirectory) {
        InputStream inputStream = null;
        try {
            Path filePath = Paths.get(zipFilePath);
            inputStream = Files.newInputStream(filePath);
            ArchiveStreamFactory archiveStreamFactory = new ArchiveStreamFactory();
            ArchiveInputStream archiveInputStream = archiveStreamFactory.createArchiveInputStream(ArchiveStreamFactory.ZIP, inputStream);
            ArchiveEntry archiveEntry = null;
            while((archiveEntry = archiveInputStream.getNextEntry()) != null) {
                Path path = Paths.get(extractDirectory, archiveEntry.getName());
                File file = path.toFile();
                if(archiveEntry.isDirectory()) {
                    if(!file.isDirectory()) {
                        file.mkdirs();
                    }
                } else {
                    File parent = file.getParentFile();
                    if(!parent.isDirectory()) {
                        parent.mkdirs();
                    }
                    try (OutputStream outputStream = Files.newOutputStream(path)) {
                        IOUtils.copy(archiveInputStream, outputStream);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ArchiveException e) {
            e.printStackTrace();
        }
    }
}

Extract .zip File into a Directory

In the following example Java program, we use the ZipFileCompressUtils class implemented above to extract a .zip file into a directory. For example we have the following data in this program.

  • “D:\SimpleSolution\sample.zip” is a zip file that needs to be extracted.
  • “D:\SimpleSolution\output” is the out directory to write extracted files.

ExtractZipFileExample.java

public class ExtractZipFileExample {
    public static void main(String[] args) {
        String zipPath = "D:\\SimpleSolution\\sample.zip";
        String outputDirectory = "D:\\SimpleSolution\\output";

        ZipFileCompressUtils zipFileCompressUtils = new ZipFileCompressUtils();

        zipFileCompressUtils.extractZip(zipPath, outputDirectory);
    }
}

Happy Coding 😊

Create .zip File in Java using Apache Commons Compress