Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท crypto

Node.js zlib.deflate() and inflate() methods

Reference for zlib.deflate(), inflate(), deflateRaw(), inflateRaw() and their sync and stream forms in Node.js: formats, preset dictionaries and error handling.

Deflate is the compression algorithm underneath gzip, PNG and ZIP. Node's zlib module exposes it in three wrappers. deflate() and inflate() use the zlib format: a 2-byte header, the compressed blocks and a 4-byte Adler-32 checksum. deflateRaw() and inflateRaw() use bare compressed blocks with no header or checksum. gzip() and gunzip() use the gzip wrapper with a CRC-32.

Pick the wrapper the other side expects. HTTP Content-Encoding: deflate means the zlib format (although some old servers sent raw data), ZIP entries and many binary protocols use raw deflate, and most file formats built on zlib use the middle option.

NODE
import { deflateSync, inflateSync } from 'node:zlib';

const packed = deflateSync('abcabcabcabc');
console.log(inflateSync(packed).toString());

Try it

Node.js Three wrappers, one algorithm

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 Preset dictionary and async form

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.deflate(buffer, options?, callback): void
zlib.inflate(buffer, options?, callback): void
zlib.deflateRaw(buffer, options?, callback): void
zlib.inflateRaw(buffer, options?, callback): void
zlib.deflateSync(buffer, options?): Buffer
zlib.inflateSync(buffer, options?): Buffer
zlib.deflateRawSync(buffer, options?): Buffer
zlib.inflateRawSync(buffer, options?): Buffer
zlib.createDeflate(options?) / createInflate(options?) / createDeflateRaw(options?) / createInflateRaw(options?): stream.Transform
Name Description
buffer A string, Buffer, typed array, DataView or ArrayBuffer.
options.level Compression level 0 to 9, or -1 for the default. Compression only.
options.dictionary A preset dictionary of likely byte sequences. Both sides must use the same one.
options.windowBits History window size as a power of two (log2), 8 to 15. When inflating, it must be at least as large as the sender's.
options.maxOutputLength Upper bound on the decompressed size in bytes. Exceeding it throws ERR_BUFFER_TOO_LARGE.
callback Receives (err, result).

Returns: sync functions return a Buffer; callback functions return nothing; the create* factories return a Transform stream (see zlib.createGzip() for streaming use).

Errors: Z_DATA_ERROR for corrupt or wrongly wrapped data (incorrect header check, invalid stored block lengths), Z_NEED_DICT when a dictionary is required but missing (Missing dictionary) or Z_DATA_ERROR when it is the wrong one, and Z_BUF_ERROR for truncated input.

Usage notes

  • Raw deflate has no integrity check. Corruption may decode into wrong output without any error, so add your own checksum (for example zlib.crc32()) when you use it.
  • A preset dictionary shines for many small, similar messages (JSON envelopes, protocol frames), where the compressor otherwise has no history to match against.
  • The Sync versions block the event loop. Use the callback or stream versions on servers.
  • For zlib-framed data of unknown origin, zlib.unzip() decides between zlib and gzip automatically but does not handle raw deflate.
  • Always bound untrusted input with maxOutputLength; see Decompression bombs.