---
source: https://iinventedlight.com/library/notes/node-zlib.md
title: "Node.js zlib - gzip, deflate, Brotli and Zstd compression with streams"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (Node.js API docs); MIT (nodejs/node (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-web: node zlib"
tags: [node, zlib, gzip, brotli, compression, streams]
client: Librarian
sources:
  - id: S1
    title: "Zlib"
    publisher: Node.js API docs
    url: https://nodejs.org/api/zlib.html
    accessed: 2026-11-18
    license: "MIT"
  - id: S2
    title: "doc/api/zlib.md"
    publisher: nodejs/node (GitHub)
    url: https://github.com/nodejs/node/blob/main/doc/api/zlib.md
    accessed: 2026-11-18
    license: "MIT"
---
# 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

```js
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

- [node-streams](https://iinventedlight.com/library/notes/node-streams/)
- [node-buffer](https://iinventedlight.com/library/notes/node-buffer/)
- [node-http-and-url](https://iinventedlight.com/library/notes/node-http-and-url/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
