Convenience methods
History
All of these take a Buffer, TypedArray, DataView, ArrayBuffer, or string
as the first argument, an optional second argument
to supply options to the zlib classes and will call the supplied callback
with callback(error, result).
Every method has a *Sync counterpart, which accept the same arguments, but
without a callback.
Unlike convenience methods, streaming decompressors (zlib.createGunzip(),
zlib.createBrotliDecompress(), zlib.createZstdDecompress(), and related
classes) do not enforce a cumulative output limit via maxOutputLength. To cap
total decoded bytes across a stream, count chunk lengths inside a
pipeline() stream.Transform stage:
import { Transform } from 'node:stream'; import { pipeline } from 'node:stream/promises'; import { createGunzip } from 'node:zlib'; function createDecodedByteLimit(maxTotalBytes) { let total = 0; return new Transform({ transform(chunk, _encoding, callback) { total += chunk.length; if (total > maxTotalBytes) { const err = new RangeError( `Decoded output exceeded ${maxTotalBytes} bytes`, ); callback(err); return; } callback(null, chunk); }, }); } await pipeline( source, createGunzip(), createDecodedByteLimit(64 * 1024), destination, );
const { Transform } = require('node:stream'); const { pipeline } = require('node:stream/promises'); const { createGunzip } = require('node:zlib'); function createDecodedByteLimit(maxTotalBytes) { let total = 0; return new Transform({ transform(chunk, _encoding, callback) { total += chunk.length; if (total > maxTotalBytes) { const err = new RangeError( `Decoded output exceeded ${maxTotalBytes} bytes`, ); callback(err); return; } callback(null, chunk); }, }); } pipeline( source, createGunzip(), createDecodedByteLimit(64 * 1024), destination, ).catch((err) => { console.error('An error occurred:', err); process.exitCode = 1; });
zlib.brotliCompress(buffer, options?, callback): void
zlib.brotliCompressSync(buffer, options?): void
Buffer | TypedArray | DataView | ArrayBuffer | stringbrotli optionsCompress a chunk of data with BrotliCompress.
zlib.brotliDecompress(buffer, options?, callback): void
zlib.brotliDecompressSync(buffer, options?): void
Buffer | TypedArray | DataView | ArrayBuffer | stringbrotli optionsDecompress a chunk of data with BrotliDecompress.
zlib.deflate(buffer, options?, callback): void
zlib.deflateSync(buffer, options?): void
Buffer | TypedArray | DataView | ArrayBuffer | stringzlib optionsCompress a chunk of data with Deflate.
zlib.deflateRaw(buffer, options?, callback): void
zlib.deflateRawSync(buffer, options?): void
Buffer | TypedArray | DataView | ArrayBuffer | stringzlib optionsCompress a chunk of data with DeflateRaw.
zlib.gunzip(buffer, options?, callback): void
zlib.gunzipSync(buffer, options?): void
Buffer | TypedArray | DataView | ArrayBuffer | stringzlib optionsDecompress a chunk of data with Gunzip.
zlib.gzip(buffer, options?, callback): void
zlib.gzipSync(buffer, options?): void
Buffer | TypedArray | DataView | ArrayBuffer | stringzlib optionsCompress a chunk of data with Gzip.
zlib.inflate(buffer, options?, callback): void
zlib.inflateSync(buffer, options?): void
Buffer | TypedArray | DataView | ArrayBuffer | stringzlib optionsDecompress a chunk of data with Inflate.
zlib.inflateRaw(buffer, options?, callback): void
zlib.inflateRawSync(buffer, options?): void
Buffer | TypedArray | DataView | ArrayBuffer | stringzlib optionsDecompress a chunk of data with InflateRaw.
zlib.unzip(buffer, options?, callback): void
zlib.unzipSync(buffer, options?): void
Buffer | TypedArray | DataView | ArrayBuffer | stringzlib optionsDecompress a chunk of data with Unzip.
zlib.zstdCompress
History
pledgedSrcSize option defaults to the byte length of buffer.zlib.zstdCompress(buffer, options?, callback): void
zlib.zstdCompressSync(buffer, options?): void
Buffer | TypedArray | DataView | ArrayBuffer | stringzstd optionsCompress a chunk of data with ZstdCompress.
zlib.zstdDecompress(buffer, options?, callback): void
zlib.zstdDecompressSync(buffer, options?): void
Buffer | TypedArray | DataView | ArrayBuffer | stringzstd optionsDecompress a chunk of data with ZstdDecompress.