Zip4j – Java Library for ZIP Files (With Examples)

In this tutorial, we show how to use the Zip4j library for handling ZIP files like adding files and directories, updating existing ZIPs, password protection, and encryption.

Table of Content:

P.S Tested with Zip4j 2.11.5

1. Adding Zip4j Dependency

To use Zip4j, we need to add the dependency to our pom.xml (Maven):

pom.xml

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

For Gradle:


dependencies {
    implementation 'net.lingala.zip4j:zip4j:2.11.5'
}

2. Creating a ZIP File and Adding Files

To create a new ZIP file and add files.

ZipExample.java

package com.mkyong.zip;

import net.lingala.zip4j.ZipFile;
import java.io.File;
import java.util.Arrays;

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

            // Add single file
            zipFile.addFile(new File("document.txt"));

            // Add multiple files
            zipFile.addFiles(Arrays.asList(
                new File("image.png"),
                new File("notes.pdf")
            ));

            System.out.println("ZIP file created successfully!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3. Adding a Directory to ZIP

To add an entire directory (including subdirectories):

ZipDirectoryExample.java

package com.mkyong.zip;

import net.lingala.zip4j.ZipFile;
import java.io.File;

public class ZipDirectoryExample {
    public static void main(String[] args) {
        try {
            ZipFile zipFile = new ZipFile("backup.zip");

            // Add directory to ZIP
            zipFile.addFolder(new File("myfolder"));

            System.out.println("Directory added to ZIP!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

4. Adding Files to an Existing ZIP

To add more files to an already existing ZIP archive:

AddToExistingZip.java

package com.mkyong.zip;

import net.lingala.zip4j.ZipFile;
import java.io.File;

public class AddToExistingZip {
    public static void main(String[] args) {
        try {
            ZipFile zipFile = new ZipFile("example.zip");

            // Add new file to existing ZIP
            zipFile.addFile(new File("newfile.txt"));

            System.out.println("File added to existing ZIP!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

5. Creating a Password-Protected ZIP

To protect a ZIP file with a password:

PasswordProtectedZip.java

package com.mkyong.zip;

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

import java.io.File;

public class PasswordProtectedZip {
    public static void main(String[] args) {
        try {
            ZipFile zipFile = new ZipFile("secured.zip", "mypassword".toCharArray());

            ZipParameters parameters = new ZipParameters();
            parameters.setEncryptFiles(true);
            parameters.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);

            zipFile.addFile(new File("confidential.txt"), parameters);

            System.out.println("Password-protected ZIP created!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

6. Creating an Encrypted ZIP (AES 256 Encryption)

For stronger encryption, AES 256-bit can be used:

AES256EncryptedZip.java

package com.mkyong.zip;

import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.AesKeyStrength;
import net.lingala.zip4j.model.enums.EncryptionMethod;

import java.io.File;

public class AES256EncryptedZip {
    public static void main(String[] args) {
        try {
            ZipFile zipFile = new ZipFile("secured.zip", "mypassword".toCharArray());

            ZipParameters parameters = new ZipParameters();
            parameters.setEncryptFiles(true);
            parameters.setEncryptionMethod(EncryptionMethod.AES);

            // Below line is optional. AES 256 is used by default. 
            // You can override it to use AES 128. AES 192 is supported only for extracting.
            parameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);

            zipFile.addFile(new File("confidential.txt"), parameters);

            System.out.println("Password-protected ZIP created!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

7. Extracting a Normal ZIP File

To extract a normal (non-password-protected) ZIP file:

ExtractZipFile.java

package com.mkyong.zip;

import net.lingala.zip4j.ZipFile;

public class ExtractZipFile {
    public static void main(String[] args) {
        try {
            ZipFile zipFile = new ZipFile("example.zip");

            // Extract to target directory
            zipFile.extractAll("output_folder");

            System.out.println("ZIP file extracted successfully!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

8. Extracting Password-Protected ZIP

To extract a password-protected ZIP file:

ExtractEncryptedZip.java

package com.mkyong.zip;

import net.lingala.zip4j.ZipFile;

public class ExtractEncryptedZip {
    public static void main(String[] args) {
        try {
            ZipFile zipFile = new ZipFile("secured.zip", "mypassword".toCharArray());

            // Extract to target directory
            zipFile.extractAll("output_folder");

            System.out.println("Files extracted successfully!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

9. References

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments