← All notes

Node.js zlib - gzip, deflate, Brotli and Zstd compression with streams

Answer: Compress a stream (such as a file) by piping the source stream through a zlib Transform stream into a destination stream. It is also possible to compress data in a single step with deflate() or gzip(). The node:zlib module provides compression implemented using Gzip, Deflate/Inflate, Brotli, and Zstd.

Examples

import { createReadStream, createWriteStream } from 'node:fs';
import { pipeline } from 'node:stream/promises';
import { createGzip, gzipSync, gunzipSync, brotliCompress } from 'node:zlib';
import { promisify } from 'node:util';

// Stream a file through gzip
await pipeline(
  createReadStream('input.txt'),
  createGzip(),
  createWriteStream('input.txt.gz'),
);

// One step on a buffer
const packed = gzipSync('hello hello hello');
gunzipSync(packed).toString();              // 'hello hello hello'

// Async Brotli without blocking the event loop
const br = promisify(brotliCompress);
const out = await br(Buffer.from('payload'));

Choosing an API

| API | Use | |---|---| | createGzip(), createDeflate(), createBrotliCompress() | Transform streams for compressing a stream such as a file | | createGunzip(), createUnzip() | decompress streams; unzip detects gzip or deflate headers | | gzip(buf, cb), deflate(buf, cb), brotliCompress(buf, cb) | compress data in a single step |

Rules

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Node.js API docs, Zlib; nodejs/node (GitHub), doc/api/zlib.md. MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Node.js API docs, Zlib; nodejs/node (GitHub), doc/api/zlib.md. MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors.

Cite this

Permanent ID: iil:note:g7wb73m3

iinventedlight. 2026. "Node.js zlib - gzip, deflate, Brotli and Zstd compression with streams." iinventedlight Library, iil:note:g7wb73m3, version of 2026-11-18. https://iinventedlight.com/library/notes/node-zlib/. License: CC-BY-4.0.

@misc{iil_note_g7wb73m3,
  author = {{iinventedlight}},
  title = {{Node.js zlib - gzip, deflate, Brotli and Zstd compression with streams}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-18},
  url = {https://iinventedlight.com/library/notes/node-zlib/},
  note = {Permanent ID iil:note:g7wb73m3. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/node-zlib.md.