How do you read and write compressed data with java.util.zip, which tool fits which job, and why must GZIPOutputStream be finished?
answer
- decorators over a byte stream
- one payload versus named entries
- local headers versus central directory
- flush leaves the compressor undrained
- final block plus eight-byte trailer
basics
~20 sUse GZIP streams for one compressed stream, ZipOutputStream and ZipInputStream for archives in sequence, and ZipFile or the zip FileSystem for random access. GZIPOutputStream must be finished, because only then are its final deflate block and trailer written.
solid answer
~40 s`java.util.zip` gives you decorators: `GZIPOutputStream` and `ZipOutputStream` wrap an `OutputStream`, `GZIPInputStream` and `ZipInputStream` wrap an `InputStream`. GZIP is one compressed stream with no entry names, so I use it for a single payload. For an archive I write with `ZipOutputStream` (`putNextEntry`, `write`, `closeEntry`), read a stream with `ZipInputStream`, and read a file with `ZipFile`, which uses the central directory to find entries by name. The zip `FileSystem` from `FileSystems.newFileSystem` exposes the archive as `Path`s and can update it. A `GZIPOutputStream` must be finished because `flush()` by default does not drain the compressor, and only `finish()` or `close()` writes the last deflate block and the 8-byte CRC and length trailer. Without them a reader hits `EOFException`. `close()` calls `finish()`, so try-with-resources is enough if you read the result after it.
code
java · 21 linesimport java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
class Gzip {
static byte[] compress(byte[] data) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try (GZIPOutputStream gzip = new GZIPOutputStream(bytes)) {
gzip.write(data);
}
return bytes.toByteArray();
}
static byte[] decompress(byte[] data) throws IOException {
try (GZIPInputStream gzip = new GZIPInputStream(new ByteArrayInputStream(data))) {
return gzip.readAllBytes();
}
}
}go deeper
Recall the two families: GZIP streams compress one run of bytes and ZIP classes handle named entries. Know the wrapping pattern, and close the stream before using the output.
Explain the lifecycle: the header at construction, data held in the Deflater, the final block and trailer at finish. Say why flush is not enough, and how ZipFile and ZipInputStream differ in what they read.
Show production judgment: an EOFException on read often points at a writer that never finished, sync flush fits live streams, entry names are untrusted input, and building GZIPInputStream first on a socket can deadlock.
Frame the format choice as a contract with consumers: a single gzip payload for transport, ZIP when entries need names and random access, and a team rule for size limits and name checks wherever archives come from outside.
## The decorators `java.util.zip` applies the wrapping idea of `java.io` to compression. **GZIP** compresses a run of bytes with no entries and no index; a file may hold several such members back to back. **ZIP** is an archive format: named entries plus a **central directory**, an index at the end of the file. - `GZIPOutputStream` extends `DeflaterOutputStream`, and `GZIPInputStream` extends `InflaterInputStream`. - `ZipOutputStream` extends `DeflaterOutputStream` too, and `ZipInputStream` extends `InflaterInputStream`. - `ZipFile` is not a stream. It implements `Closeable`, opens a file, and only reads. - The zip `FileSystem` is not in `java.util.zip` at all. The `jdk.zipfs` module provides it through `FileSystems.newFileSystem`. ## Why a GZIPOutputStream must be finished The lifecycle runs in this order: 1. The constructor writes the 10-byte header to the wrapped stream at once. 2. `write` feeds a `Deflater` and updates a CRC-32. The `Deflater` holds input until it has output to give, so writing 32 bytes usually adds nothing to the wrapped stream. 3. `finish()` tells the `Deflater` the input is over, drains the **final deflate block**, and writes the 8-byte **trailer**: the CRC-32 and the uncompressed size modulo 2^32. 4. `close()` calls `finish()`, ends the `Deflater` and closes the wrapped stream. The reader needs the bytes from step 3. `GZIPInputStream` checks the trailer against its own CRC and byte count, and a stream that stops early fails with `EOFException: Unexpected end of ZLIB input stream`. A trailer that does not match gives `ZipException: Corrupt GZIP trailer`. **`flush()` does not rescue it.** By default it flushes only the wrapped stream and leaves the compressor alone: write 32 bytes, flush, and the wrapped stream still holds only the header. `new GZIPOutputStream(out, true)` turns on `syncFlush`, so `flush()` first forces pending data out with `Deflater.SYNC_FLUSH`. A reader can then decode everything so far, and still gets `EOFException` at the end of the stream, because the final block and trailer are missing. Sync flush keeps a live stream moving; it does not end one. Two more traps: - `finish()` leaves the wrapped stream open, which suits writing more after it, such as another gzip member. A later `write` on the same object throws `IOException: write beyond end of stream`. - `bytes.toByteArray()` inside the try block returns the state before `close()`, usually just the header. Read it after the block. ## Which tool for which job | Job | Use | Why | Limit | |---|---|---|---| | One payload, such as a file or a request body | `GZIPOutputStream`, `GZIPInputStream` | Just a compressed stream | No entries; the JDK's header stores no file name | | Write an archive to any `OutputStream` | `ZipOutputStream` | Entries one after another | Sequential; a duplicate name gives `ZipException` | | Read an archive that arrives as a stream | `ZipInputStream` | Needs no seeking | Sees local headers only, so no external file attributes | | Look up entries in a file on disk | `ZipFile` | Reads the central directory; `getEntry(name)`, `stream()` | Read-only; constructors take a `File` or a name, not a stream | | Treat an archive as a tree, or update it | Zip `FileSystem` | `Path`s work with `Files` | Changes reach the archive when it closes | ## ZIP's own finishing rule `ZipOutputStream.finish()` writes the central directory and its end record, and `close()` calls it. If neither runs, the readers disagree: - `ZipFile` refuses the file with `ZipException: zip END header not found`, because it starts from the end record. - `ZipInputStream` still reads the entries that were completed and written out, because it walks local headers from the front. The same difference explains a sizing surprise: for a deflated entry written by `ZipOutputStream`, `ZipInputStream` reports `getSize()` as -1 until the entry has been read, while `ZipFile` knows it up front. And `ZipInputStream` returns `null` from `getNextEntry()` on input that is not a ZIP at all, so a wrong file looks like an empty archive, while `ZipFile` throws. ## The zip FileSystem and entry names `FileSystems.newFileSystem(path)` opens an existing archive, and `Map.of("create", "true")` creates one. Inside, `fs.getPath("/a.txt")` works with `Files.copy`, `Files.walk` and the rest. The file on disk does not change until `fs.close()`, so check `Files.size(zip)` before and after to see it. Entry names are untrusted data. `ZipFile` and `ZipInputStream` return a name like `../evil.txt` unchanged, so extraction code must resolve it against the target directory, normalize it and check that it still starts with the target. The zip provider refuses to open an archive with a `.` or `..` name element.
- A client and a server both wrap a socket's input in GZIPInputStream before anything else. What goes wrong?They can deadlock. The `GZIPInputStream` constructor reads the header, so it blocks until the peer has sent one, and each side is waiting for the other. Build the `GZIPOutputStream` first, since its constructor writes the header, and flush anything buffering it. For messages sent back and forth, use `syncFlush` too, or each message stays inside the compressor.
- What does GZIPInputStream do when several gzip members are concatenated in one stream?It reads them as one stream of data, with each read returning data from at most one member. At JDK 27 it calls `available()` on the wrapped stream to decide whether another member follows, unless `jdk.util.gzip.tryReadAheadAfterTrailer` is true, so a stream reporting 0 can end it early. JDK 25 tries the next header without asking. Both treat malformed bytes after a member as the end.
- Why would ZipFile reject an archive that ZipInputStream reads without complaint?Usually the writer never reached `finish()` or `close()`. `ZipFile` starts from the end record and the central directory, so without them it throws `ZipException`. `ZipInputStream` reads local headers from the front and never needs that index, so it returns the completed entries. A lenient read here does not prove that the archive is whole.
- How do you extract a ZIP safely when entry names are untrusted?Treat each name as input. `ZipFile` and `ZipInputStream` return a name such as `../evil.txt` as written, so compute `target.resolve(name).normalize()` and check `startsWith(target)` before writing. Cap total extracted size as well, since a small archive can inflate to a huge output.
- Calling flush() on a GZIPOutputStream makes the output so far a valid gzip file.
- With syncFlush on, close() is optional because a reader can decode everything.
- GZIP stores several named files, so it can replace ZIP for archives.
- ZipInputStream reads the central directory, so every entry's size is known up front.
- ZipFile can read from any InputStream, such as an HTTP response body.
- finish() closes the wrapped stream, so calling close() afterwards adds nothing.
- GZIPOutputStream and GZIPInputStream
- Java 1.1
- ZipOutputStream, ZipInputStream and ZipFile
- Java 1.1
- GZIPOutputStream syncFlush constructors
- Java 1.7
- FileSystems.newFileSystem(Path) and (Path, Map)
- Java 13
- GZIPInputStream available() check and its property
- Java 27
Assumes JDK 27; JDK 25's GZIPInputStream looks for a following member without first calling available().
context
- GZIPOutputStream.java at jdk-27-gagithub.com
- ZipInputStream (Java SE 27 & JDK 27)docs.oracle.com
- ZipFileSystem.java at jdk-27-gagithub.com