Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท crypto

Node.js zlib.createGzip() method

Reference for zlib.createGzip() in Node.js: a Transform stream that gzips data piece by piece, with pipeline(), flush(), options, backpressure and error handling.

zlib.createGzip() returns a Gzip object, a Transform stream: bytes written to it come out compressed in the gzip format. Because it works in chunks, memory use stays small whatever the size of the data, which makes it the right tool for compressing files, HTTP responses and anything else that arrives over time.

The usual way to use it is inside stream.pipeline(), between a source (a file, a request body, a generator) and a destination (a file, a socket, a response). Sibling factories follow the same shape: createDeflate(), createDeflateRaw(), createBrotliCompress() and createZstdCompress().

NODE
import { createGzip } from 'node:zlib';
import { pipeline } from 'node:stream/promises';

await pipeline(source, createGzip(), destination);

Try it

Node.js Compressing a stream of lines

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.

Node.js Files and flush()

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

NODE
zlib.createGzip(options?: object): zlib.Gzip   // a stream.Transform
gzip.flush(kind?: number, callback?: () => void): void
gzip.params(level: number, strategy: number, callback: (err?: Error) => void): void
gzip.reset(): void
gzip.close(callback?: () => void): void
Name Description
options.level 0 (no compression) to 9 (best), -1 for the default (6).
options.memLevel Memory use, 1 to 9. Default 8.
options.strategy Z_DEFAULT_STRATEGY, Z_FILTERED, Z_HUFFMAN_ONLY, Z_RLE or Z_FIXED.
options.windowBits History window size (log2), 9 to 15. Default 15.
options.chunkSize Size of output chunks. Default 16384.
options.flush, options.finishFlush Flush modes applied during and at the end of the stream.

Returns: a Gzip stream. It emits data, end, error and the usual Transform events; it also exposes bytesWritten.

Errors: compression itself rarely fails. Errors come from invalid options (thrown at construction, ERR_OUT_OF_RANGE or ERR_INVALID_ARG_VALUE) or from the surrounding pipeline.

Usage notes

  • Prefer pipeline() (from node:stream/promises) over bare .pipe(): it forwards errors, destroys every stream on failure and waits for completion.
  • The stream honors backpressure. The compressor holds up to a few chunks internally, then asks the writer to wait.
  • Gzip buffers data to find matches. If you stream live messages (server-sent events, logs) call flush(constants.Z_SYNC_FLUSH) after each message so the client sees it now. Flushing often hurts the ratio.
  • Compress an HTTP response only when the client sent Accept-Encoding: gzip, set Content-Encoding: gzip and add Vary: Accept-Encoding. Skip content that is already compressed (images, videos, .zip).
  • Streams cannot know the final size up front, so a gzipped HTTP response uses chunked transfer encoding and has no Content-Length.
  • Call end() to write the trailer. A gzip file that was never ended is truncated and fails to decompress.
  • params() lets you change the level or strategy mid-stream, at the cost of an internal flush.