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.
try (var out = new GZIPOutputStream(Files.newOutputStream(path))) { out.write(bytes); }
Try it
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.
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
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()orfinish()writes the trailer. Forgetting it produces a truncated file. - Zip-slip. An entry name such as
../../evilis legal in the format. When extracting, resolve the name against the target directory, normalize it, and checkstartsWith(target)before writing. - Streaming versus
ZipFile.ZipInputStreamreads once from start to end.ZipFilereads 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_COMPRESSIONis 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
Charsetto 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.zipdoes not do 7z, RAR or tar. For editing a ZIP like a file system,FileSystems.newFileSystem(zipPath)mounts it as aPathtree. readAllBytes()on an entry stream (Java 9+) is fine for small entries; copy in chunks for large ones.