Create .zip File in Java using Apache Commons Compress
Tags: Apache Commons Apache Commons Compress ZipArchiveEntry ZipArchiveOutputStream IOUtils File Java IO Java NIO
In this Java tutorial, we learn how to use the Apache Commons Compress library to create a .zip file 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 createZipFile() public method to create a .zip file from a file or directory source.
ZipFileCompressUtils.java
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
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 createZipFile(String zipFileName, String fileOrDirectoryToZip) {
BufferedOutputStream bufferedOutputStream = null;
ZipArchiveOutputStream zipArchiveOutputStream = null;
OutputStream outputStream = null;
try {
Path zipFilePath = Paths.get(zipFileName);
outputStream = Files.newOutputStream(zipFilePath);
bufferedOutputStream = new BufferedOutputStream(outputStream);
zipArchiveOutputStream = new ZipArchiveOutputStream(bufferedOutputStream);
File fileToZip = new File(fileOrDirectoryToZip);
addFileToZipStream(zipArchiveOutputStream, fileToZip, "");
zipArchiveOutputStream.close();
bufferedOutputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void addFileToZipStream(ZipArchiveOutputStream zipArchiveOutputStream, File fileToZip, String base) throws IOException {
String entryName = base + fileToZip.getName();
ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(fileToZip, entryName);
zipArchiveOutputStream.putArchiveEntry(zipArchiveEntry);
if(fileToZip.isFile()) {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(fileToZip);
IOUtils.copy(fileInputStream, zipArchiveOutputStream);
zipArchiveOutputStream.closeArchiveEntry();
} finally {
IOUtils.closeQuietly(fileInputStream);
}
} else {
zipArchiveOutputStream.closeArchiveEntry();
File[] files = fileToZip.listFiles();
if(files != null) {
for (File file: files) {
addFileToZipStream(zipArchiveOutputStream, file, entryName + "/");
}
}
}
}
}
Create New .zip File from a Directory
In the following example Java program, we use the above class to create a new file file. In the example we have
- “D:\SimpleSolution\sample.zip” is the .zip file that is expected to be created.
- “D:\SimpleSolution\sample\” is the directory that needs to be zip.
CreateZipFileFromDirectoryExample.java
public class CreateZipFileFromDirectoryExample {
public static void main(String[] args) {
String zipPath = "D:\\SimpleSolution\\sample.zip";
String directoryToZip = "D:\\SimpleSolution\\sample\\";
ZipFileCompressUtils zipFileCompressUtils = new ZipFileCompressUtils();
zipFileCompressUtils.createZipFile(zipPath, directoryToZip);
}
}
Create New .zip File from a File
In the following example Java program, we create .zip file from a source file for example “D:\SimpleSolution\sample\test.txt”
CreateZipFileFromFileExample.java
public class CreateZipFileFromFileExample {
public static void main(String[] args) {
String zipPath = "D:\\SimpleSolution\\sample.zip";
String fileToZip = "D:\\SimpleSolution\\sample\\test.txt";
ZipFileCompressUtils zipFileCompressUtils = new ZipFileCompressUtils();
zipFileCompressUtils.createZipFile(zipPath, fileToZip);
}
}
Happy Coding 😊