Java
Zip/Unzip Files
Java Library
Programming
File Compression

What is a good Java library to zip/unzip files?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

The best Java library for zip and unzip work depends on what you actually need. If you only need ordinary ZIP support, the standard JDK classes are often enough. If you need broader archive support or a friendlier abstraction over many formats, Apache Commons Compress is the usual next recommendation.

Start with the JDK for Plain ZIP Files

Java already includes ZIP support in java.util.zip. For many applications, adding an external dependency is unnecessary.

Example: create a ZIP file with the standard library.

java
1import java.io.IOException;
2import java.io.InputStream;
3import java.io.OutputStream;
4import java.nio.file.Files;
5import java.nio.file.Path;
6import java.util.zip.ZipEntry;
7import java.util.zip.ZipOutputStream;
8
9public class ZipExample {
10    public static void main(String[] args) throws IOException {
11        Path source = Path.of("notes.txt");
12        Path zipPath = Path.of("archive.zip");
13
14        try (OutputStream fos = Files.newOutputStream(zipPath);
15             ZipOutputStream zos = new ZipOutputStream(fos);
16             InputStream in = Files.newInputStream(source)) {
17
18            zos.putNextEntry(new ZipEntry(source.getFileName().toString()));
19            in.transferTo(zos);
20            zos.closeEntry();
21        }
22    }
23}

That is simple, dependency-free, and perfectly good for many common tasks.

Unzipping Safely Matters More Than the Library Name

When extracting archives, security matters. A ZIP entry can contain ../ path segments and try to escape the target directory. This is the classic zip-slip problem.

A safe extraction example with the JDK looks like this:

java
1import java.io.IOException;
2import java.io.InputStream;
3import java.nio.file.Files;
4import java.nio.file.Path;
5import java.util.zip.ZipEntry;
6import java.util.zip.ZipInputStream;
7
8public class UnzipExample {
9    public static void main(String[] args) throws IOException {
10        Path zipPath = Path.of("archive.zip");
11        Path targetDir = Path.of("output");
12        Files.createDirectories(targetDir);
13
14        try (InputStream fis = Files.newInputStream(zipPath);
15             ZipInputStream zis = new ZipInputStream(fis)) {
16
17            ZipEntry entry;
18            while ((entry = zis.getNextEntry()) != null) {
19                Path outputPath = targetDir.resolve(entry.getName()).normalize();
20                if (!outputPath.startsWith(targetDir)) {
21                    throw new IOException("Blocked zip-slip attempt: " + entry.getName());
22                }
23
24                if (entry.isDirectory()) {
25                    Files.createDirectories(outputPath);
26                } else {
27                    Files.createDirectories(outputPath.getParent());
28                    Files.copy(zis, outputPath);
29                }
30                zis.closeEntry();
31            }
32        }
33    }
34}

That safety check is more important than whether you picked library A or library B.

When Apache Commons Compress Is Better

Apache Commons Compress becomes attractive when you need more than plain ZIP handling. It supports additional archive and compression formats, and many teams prefer its archive-oriented API for nontrivial tooling.

It is a strong choice when you need:

  • tar, gzip, bzip2, or other formats in one library
  • archive processing beyond basic ZIP files
  • a consistent abstraction across multiple formats

So the practical answer is often:

  • use the JDK for simple ZIP-only work
  • use Commons Compress when format support or archive tooling grows beyond that

What About Encrypted ZIP Files?

If your main requirement is password-protected ZIP archives, many developers reach for a dedicated library such as Zip4j instead of forcing the JDK APIs to cover that use case. That is a narrower recommendation, but it is worth knowing because encrypted ZIP support is often the real reason the built-in APIs feel insufficient.

Common Pitfalls

  • Pulling in a large external dependency when java.util.zip already solves the problem.
  • Extracting archives without protecting against zip-slip paths.
  • Assuming zip and gzip are the same thing. ZIP is an archive format, while gzip is primarily compression.
  • Choosing a library by popularity alone instead of by feature requirements.
  • Forgetting that password-protected ZIP files are a special case that may need a dedicated library.

Summary

  • For normal ZIP read and write operations, the JDK is often enough.
  • Apache Commons Compress is a strong choice when you need more archive formats or broader tooling.
  • Safe extraction is critical regardless of which library you use.
  • Zip-slip protection matters more than convenience API style.
  • If encryption is the main requirement, consider a ZIP-focused library built for that case.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.