Node.js Reference ยท crypto
Node.js zlib.gzip() and gunzip() methods
Reference for zlib.gzip(), gunzip(), gzipSync(), gunzipSync() and unzip() in Node.js: compress and decompress buffers, options, error codes and multi-member input.
zlib.gzip() compresses a string or buffer into the gzip format, the same format as .gz files and the Content-Encoding: gzip HTTP header. zlib.gunzip() reverses it. Both take the whole input at once and produce the whole output at once, so they suit small and medium payloads such as JSON documents, cache entries and HTTP bodies.
Each function exists in three styles: a callback form (gzip, gunzip), a blocking form (gzipSync, gunzipSync) and, through util.promisify, a promise form. The callback and promise forms run the compression on the libuv thread pool, so the event loop stays free. The sync forms block it.
import { gzipSync, gunzipSync } from 'node:zlib'; const packed = gzipSync('hello hello hello'); console.log(gunzipSync(packed).toString());
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 node:readline or process.stdin), 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 node:readline or process.stdin), it gets the next line from this box.
Syntax
zlib.gzip(buffer: string | Buffer | TypedArray | DataView | ArrayBuffer, options?: object, callback: (err: Error | null, result: Buffer) => void): void zlib.gunzip(buffer, options?, callback): void zlib.gzipSync(buffer, options?): Buffer zlib.gunzipSync(buffer, options?): Buffer zlib.unzip(buffer, options?, callback): void zlib.unzipSync(buffer, options?): Buffer
| Name | Description |
|---|---|
buffer |
The data to process. Strings are encoded as UTF-8. |
options |
Optional settings shared by all zlib classes, such as level, memLevel, strategy, windowBits, chunkSize, dictionary, maxOutputLength and finishFlush. See zlib options. |
callback |
Called with an error or a Buffer holding the result. |
Returns: the callback functions return nothing; the sync functions return a Buffer.
Errors: invalid input produces an error whose code is Z_DATA_ERROR (with messages such as incorrect header check) or Z_BUF_ERROR (with unexpected end of file) for truncated input. Exceeding maxOutputLength throws ERR_BUFFER_TOO_LARGE. Bad argument types throw ERR_INVALID_ARG_TYPE. The sync functions throw; the async ones pass the error to the callback.
Usage notes
- Gzip adds a 10-byte header and an 8-byte trailer (CRC-32 and original size). Tiny inputs can come out larger than they went in.
gunzip()accepts several concatenated gzip members and joins their output, which is what thegzipcommand-line tool does too.unzip()accepts either gzip or zlib-wrapped deflate data and picks by the header. It does not understand.ziparchives.- Prefer the async forms inside servers. A large
gzipSync()call blocks every other request for as long as it runs. For files and sockets use streams (zlib.createGzip()). - Always limit what you decompress when the input comes from outside. Pass
maxOutputLength; see Decompression bombs. - Compression is deterministic for a given Node and zlib version, but output bytes can differ between versions or platforms, so never hash compressed bytes expecting stability.
- The default level is
Z_DEFAULT_COMPRESSION(6). Levels above 6 rarely pay off; below 3 trade ratio for speed.