On this page

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;
});
M

zlib.brotliCompress

History
zlib.brotliCompress(buffer, options?, callback): void
Attributes
M

zlib.brotliCompressSync

History
zlib.brotliCompressSync(buffer, options?): void
Attributes

Compress a chunk of data with BrotliCompress.

M

zlib.brotliDecompress

History
zlib.brotliDecompress(buffer, options?, callback): void
Attributes
M

zlib.brotliDecompressSync

History
zlib.brotliDecompressSync(buffer, options?): void
Attributes

Decompress a chunk of data with BrotliDecompress.

zlib.deflate(buffer, options?, callback): void
Attributes
zlib.deflateSync(buffer, options?): void
Attributes

Compress a chunk of data with Deflate.

zlib.deflateRaw(buffer, options?, callback): void
Attributes
zlib.deflateRawSync(buffer, options?): void
Attributes

Compress a chunk of data with DeflateRaw.

zlib.gunzip(buffer, options?, callback): void
Attributes
zlib.gunzipSync(buffer, options?): void
Attributes

Decompress a chunk of data with Gunzip.

zlib.gzip(buffer, options?, callback): void
Attributes
zlib.gzipSync(buffer, options?): void
Attributes

Compress a chunk of data with Gzip.

zlib.inflate(buffer, options?, callback): void
Attributes
zlib.inflateSync(buffer, options?): void
Attributes

Decompress a chunk of data with Inflate.

zlib.inflateRaw(buffer, options?, callback): void
Attributes
zlib.inflateRawSync(buffer, options?): void
Attributes

Decompress a chunk of data with InflateRaw.

zlib.unzip(buffer, options?, callback): void
Attributes
zlib.unzipSync(buffer, options?): void
Attributes

Decompress a chunk of data with Unzip.

zlib.zstdCompress(buffer, options?, callback): void
Stability: 1Experimental
Attributes
M

zlib.zstdCompressSync

History
zlib.zstdCompressSync(buffer, options?): void
Stability: 1Experimental
Attributes

Compress a chunk of data with ZstdCompress.

M

zlib.zstdDecompress

History
zlib.zstdDecompress(buffer, options?, callback): void
Stability: 1Experimental
Attributes
M

zlib.zstdDecompressSync

History
zlib.zstdDecompressSync(buffer, options?): void
Stability: 1Experimental
Attributes

Decompress a chunk of data with ZstdDecompress.