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
- Compression and decompression are built around the Node.js Streams API.
- All zlib APIs, except those that are explicitly synchronous, use the Node.js internal threadpool.
- Creating a large number of zlib objects at the same time can cause significant memory fragmentation. Cache compression results to avoid duplication of effort.
- The module can implement the gzip, deflate, br, and zstd content-encoding mechanisms defined by HTTP, based on the request's Accept-Encoding header.
See also
Claims
- id: C1 claim: zlib supports Gzip, Deflate, Brotli and Zstd. source: S1 evidence: "provides compression functionality implemented using Gzip, Deflate/Inflate, Brotli, and Zstd."
- id: C2 claim: Non-sync zlib APIs use the threadpool. source: S1 evidence: "except those that are explicitly synchronous, use the Node.js internal threadpool."
- id: C3 claim: zlib is built on streams. source: S2 evidence: "Compression and decompression are built around the Node.js"
Sources
- [S1] Zlib. Node.js API docs. https://nodejs.org/api/zlib.html . License: MIT. Accessed 2026-11-18.
- [S2] doc/api/zlib.md. nodejs/node (GitHub). https://github.com/nodejs/node/blob/main/doc/api/zlib.md . License: MIT. Accessed 2026-11-18.
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
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.},
}
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.
- node-zlib.table.json (table; CC-BY-4.0)
- node-zlib.schema.json (schema; CC-BY-4.0)
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.