Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท I/O and Files

ZipOutputStream and GZIPOutputStream

Reference for java.util.zip: compress data with GZIPOutputStream, build and read ZIP archives with ZipOutputStream and ZipInputStream, and avoid zip-slip.

The java.util.zip package compresses data with the DEFLATE algorithm. There are two formats. GZIP compresses a single stream of bytes (think file.txt.gz). ZIP is an archive that holds many named entries, each compressed separately.

You use these classes as stream decorators. GZIPOutputStream wraps any OutputStream and compresses what you write; GZIPInputStream wraps an InputStream and decompresses what you read. ZipOutputStream and ZipInputStream do the same for archives, with putNextEntry() and getNextEntry() marking where each file starts.

Java
try (var out = new GZIPOutputStream(Files.newOutputStream(path))) {
    out.write(bytes);
}

Try it

Java GZIP round trip in memory

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Writing and reading a ZIP archive

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public class GZIPOutputStream extends DeflaterOutputStream
GZIPOutputStream(OutputStream out) throws IOException
GZIPInputStream(InputStream in) throws IOException

public class ZipOutputStream extends DeflaterOutputStream
ZipOutputStream(OutputStream out)
ZipInputStream(InputStream in)
Member Description
ZipOutputStream.putNextEntry(ZipEntry e) Starts a new entry; the following writes belong to it.
ZipOutputStream.closeEntry() Finishes the current entry.
ZipOutputStream.setLevel(int) / setComment(String) / setMethod(int) Compression level (0 to 9), archive comment, DEFLATED or STORED.
ZipInputStream.getNextEntry() Moves to the next entry; null when there are none.
ZipEntry(String name) An entry; a name ending in / is a directory.
ZipFile(File) / getEntry(name) / getInputStream(entry) / entries() Random access to an archive on disk.
GZIPOutputStream.finish() Writes the trailer without closing the underlying stream.

Throws: IOException for most methods, ZipException (a subclass) for a duplicate entry name or a corrupt archive, EOFException or ZipException: Not in GZIP format when reading data that is not gzip.

Usage notes

  • Close to finish. Compressed data is incomplete until close() or finish() writes the trailer. Forgetting it produces a truncated file.
  • Zip-slip. An entry name such as ../../evil is legal in the format. When extracting, resolve the name against the target directory, normalize it, and check startsWith(target) before writing.
  • Streaming versus ZipFile. ZipInputStream reads once from start to end. ZipFile reads the central directory and gives random access, but needs a file on disk.
  • Level versus size. The default level is a good compromise. setLevel(Deflater.BEST_COMPRESSION) costs time for a small gain; NO_COMPRESSION is for already-compressed files such as JPEG.
  • Entry names use / as the separator on every platform. Do not add a leading /.
  • Charset. Names are UTF-8 by default. Pass a Charset to the constructors to read old archives that used another encoding.
  • Zip bombs. A small archive can expand to gigabytes. Limit the total number of bytes you accept when handling untrusted input.
  • Other formats. java.util.zip does not do 7z, RAR or tar. For editing a ZIP like a file system, FileSystems.newFileSystem(zipPath) mounts it as a Path tree.
  • readAllBytes() on an entry stream (Java 9+) is fine for small entries; copy in chunks for large ones.