Java ZIP Library to Create & Manage ZIP Archive

Open Source Java Library that Supports Creating, Extracting, and Modifying ZIP Files Programmatically. It Supports Unicode, Splitting and Merging Archives via Java API.

In the world of Java programming, handling zip archives efficiently is a common requirement. Whether it's for compressing files for storage or transferring data over networks, a reliable library can make all the difference. One such gem in the Java ecosystem is the Zip4j library, a powerful tool for working with zip files. Let's delve into what makes Zip4j a go-to choice for developers and explore its features and benefits. Developed by Srikanth Lingala, Zip4j simplifies complex zip file operations, offering developers a seamless way to work with archives within their Java applications.

Zip4j is an open source Java library that provides an easy-to-use interface for creating, extracting, and modifying zip files programmatically. The Zip4j library provides a straightforward API that allows software developers to perform common ZIP file operations with minimal code. Whether it's creating a new ZIP file, extracting files, or updating an existing archive, the library's intuitive design simplifies the implementation process. The library supports Read/Write password protected zip files and streams as well as supports for Zip64 format.

The Zip4j library proves to be a valuable asset for Java developers seeking a reliable and user-friendly library for ZIP file manipulation. The library supports a wide range of compression methods, including Deflate, Store, BZip2, and AES encryption. This flexibility allows developers to choose the compression method that best suits their requirements. Its ease of use, encryption capabilities, and support for various features make it a top choice for projects involving compression and decompression tasks. As software developers continue to explore efficient ways of handling ZIP files in Java, Zip4j stands out as a robust and versatile solution.

Previous Next

Getting Started with Zip4j

The recommend way to use Zip4j is via Maven repository. You can easily use Zip4j API directly in your Maven Projects with simple configurations.

Jarchivelib Maven Dependency

<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j </artifactId>
<version>2.11.5</version>
<type>jar</type> <dependency>

Install via Git command

 git clone https://github.com/srikanth-lingala/zip4j.git 

You can download the library directly from GitHub

Zip File Creation using Java Library

With Zip4j, creating new ZIP files is a breeze. The library enables software developers to create as well as edit a ZIP archive with just a couple of lines of Java code. The library provides an intuitive API for adding files to a new or existing ZIP archive. It is also supports several advanced features like adding, extracting, updating, removing files from a zip file programmatically, applying Unicode, splitting and merging archives inside Java applications. The following example shows how software developers can create a new ZIP archive and add files to it using java commands.

How to Create a New ZIP Archive and Add Files to It via Java API?

import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.model.ZipParameters;

public class ZipCreationExample {

    public static void main(String[] args) {
        try {
            // Create a new ZipFile object
            ZipFile zipFile = new ZipFile("example.zip");

            // Specify parameters for the file to be added
            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // Set compression method
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // Set compression level

            // Add a file to the ZIP archive
            zipFile.addFile("file.txt", parameters);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Extracting Files from ZIP Archives via Java

The open source Zip4j simplifies the process of extracting files from ZIP archives, providing an easy-to-use API for decompression. Software developers can easily select a ZIP file of their choice as well as the out directory. It is also possible to extract a single file from a ZIP archive with a new name and location. The following example demonstrates how easily software developers can extract files from ZIP file to a destination direction inside Java applications.

How to Extract Files from ZIP Archive inside Java Applications?

import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.model.ZipParameters;

public class ExtractionExample {

    public static void main(String[] args) {
        try {
            // Specify the path to the ZIP archive
            ZipFile zipFile = new ZipFile("example.zip");

            // Specify the destination directory for extraction
            zipFile.extractAll("extracted_files/");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Splitting and Merging Archives via Java

Large files can be challenging to handle, but Zip4j makes it easier by allowing software developers to split ZIP files into smaller parts inside Java applications. This feature is beneficial for transferring or storing large archives efficiently. Additionally, the library enables the merging of split archives, restoring the original ZIP file seamlessly. The following method can be used to merge a zip file which is split across several files into a single zip file.

How to Merge ZIP Files into a Single ZIP File via Java API?


new ZipFile("split_zip_file.zip").mergeSplitFiles(new File("merged_zip_file.zip"));

Apply Encryption and Password Protection

Security is a critical aspect of file compression, and Zip4j excels in this area. Software developers can easily encrypt and password-protect their ZIP files, ensuring that sensitive data remains secure. The library supports various encryption algorithms, including AES (Advanced Encryption Standard). Zip4j enables developers to encrypt ZIP files with strong encryption algorithms and protect them with passwords as given in the following code example.

How to Secure Data by Encrypting ZIP Files using Java API?


import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.model.ZipParameters;

public class EncryptionExample {

    public static void main(String[] args) {
        try {
            // Create a new ZipFile object
            ZipFile zipFile = new ZipFile("encrypted.zip");

            // Specify encryption parameters
            ZipParameters parameters = new ZipParameters();
            parameters.setEncryptFiles(true);
            parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES); // Use AES encryption
            parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256); // Set key strength
            parameters.setPassword("secretpassword"); // Set password

            // Add files to the ZIP archive with encryption
            zipFile.addFile("file.txt", parameters);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}