Programming & Development / April 18, 2025

Best Java Libraries for Zip/Unzip (Simple to Advanced)

Java zip unzip file compress file Java extract zip Java zip4j Apache Compress java.util.zip

🧰 1. java.util.zip – Built-in, Lightweight

✅ No dependencies

📦 Great for basic zip/unzip tasks

📌 Zip File

java

import java.io.*;
import java.util.zip.*;

public class ZipUtil {
    public static void zipFile(String sourcePath, String zipPath) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(zipPath);
             ZipOutputStream zos = new ZipOutputStream(fos);
             FileInputStream fis = new FileInputStream(sourcePath)) {

            ZipEntry entry = new ZipEntry(new File(sourcePath).getName());
            zos.putNextEntry(entry);

            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }
        }
    }
}

📌 Unzip File

java

public static void unzipFile(String zipPath, String targetDir) throws IOException {
    try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipPath))) {
        ZipEntry entry = zis.getNextEntry();
        byte[] buffer = new byte[1024];
        while (entry != null) {
            File outFile = new File(targetDir, entry.getName());
            if (entry.isDirectory()) {
                outFile.mkdirs();
            } else {
                outFile.getParentFile().mkdirs();
                try (FileOutputStream fos = new FileOutputStream(outFile)) {
                    int len;
                    while ((len = zis.read(buffer)) > 0) {
                        fos.write(buffer, 0, len);
                    }
                }
            }
            entry = zis.getNextEntry();
        }
    }
}

⚙️ 2. Apache Commons Compress – More Control + Formats

✅ More formats (tar, gzip, etc.)

🧩 Requires Maven:

xml

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

📌 Example Zip & Unzip

java

import org.apache.commons.compress.archivers.zip.*;
import org.apache.commons.compress.archivers.ArchiveEntry;

try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(new File("file.zip"));
     FileInputStream fis = new FileInputStream("source.txt")) {

    ZipArchiveEntry entry = new ZipArchiveEntry("source.txt");
    zos.putArchiveEntry(entry);

    byte[] buffer = new byte[1024];
    int len;
    while ((len = fis.read(buffer)) != -1) {
        zos.write(buffer, 0, len);
    }
    zos.closeArchiveEntry();
}

🔐 3. Zip4j – Best for Advanced Features (e.g., Passwords)

✅ Password-protected zips

✅ AES encryption

⚙️ Simple API

📦 Maven

xml

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

📌 Zip & Unzip

java

import net.lingala.zip4j.ZipFile;

ZipFile zipFile = new ZipFile("compressed.zip");
zipFile.addFile(new File("source.txt")); // zip

zipFile.extractAll("outputDir"); // unzip

🔐 With Password

java

ZipFile zipFile = new ZipFile("secure.zip", "secret123".toCharArray());
zipFile.addFile(new File("confidential.txt"));

🧭 Summary Table

LibraryStrengthsPasswordsBuilt-in?Formatsjava.util.zipBasic zip/unzip, no deps❌✅zipApache Commons CompressMultiple formats, robust API❌❌zip, tar, etc.Zip4jEasiest API, password + AES encryption✅❌zip only


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex