Skip to main content
zlib - Node documentation

Usage in Deno

import * as mod from "node:zlib";

The node:zlib module provides compression functionality implemented using Gzip, Deflate/Inflate, and Brotli.

To access it:

import zlib from 'node:zlib';

Compression and decompression are built around the Node.js Streams API.

Compressing or decompressing a stream (such as a file) can be accomplished by piping the source stream through a zlib Transform stream into a destination stream:

import { createGzip } from 'node:zlib';
import { pipeline } from 'node:stream';
import {
  createReadStream,
  createWriteStream,
} from 'node:fs';

const gzip = createGzip();
const source = createReadStream('input.txt');
const destination = createWriteStream('input.txt.gz');

pipeline(source, gzip, destination, (err) => {
  if (err) {
    console.error('An error occurred:', err);
    process.exitCode = 1;
  }
});

// Or, Promisified

import { promisify } from 'node:util';
const pipe = promisify(pipeline);

async function do_gzip(input, output) {
  const gzip = createGzip();
  const source = createReadStream(input);
  const destination = createWriteStream(output);
  await pipe(source, gzip, destination);
}

do_gzip('input.txt', 'input.txt.gz')
  .catch((err) => {
    console.error('An error occurred:', err);
    process.exitCode = 1;
  });

It is also possible to compress or decompress data in a single step:

import { deflate, unzip } from 'node:zlib';

const input = '.................................';
deflate(input, (err, buffer) => {
  if (err) {
    console.error('An error occurred:', err);
    process.exitCode = 1;
  }
  console.log(buffer.toString('base64'));
});

const buffer = Buffer.from('eJzT0yMAAGTvBe8=', 'base64');
unzip(buffer, (err, buffer) => {
  if (err) {
    console.error('An error occurred:', err);
    process.exitCode = 1;
  }
  console.log(buffer.toString());
});

// Or, Promisified

import { promisify } from 'node:util';
const do_unzip = promisify(unzip);

do_unzip(buffer)
  .then((buf) => console.log(buf.toString()))
  .catch((err) => {
    console.error('An error occurred:', err);
    process.exitCode = 1;
  });

Functions

f
brotliCompress
No documentation available
f
brotliCompressSync

Compress a chunk of data with BrotliCompress.

f
brotliDecompress
No documentation available
f
brotliDecompressSync

Decompress a chunk of data with BrotliDecompress.

f
crc32

Computes a 32-bit Cyclic Redundancy Check checksum of data.If value is specified, it is used as the starting value of the checksum, otherwise, 0 is used as the starting value.

f
createBrotliCompress

Creates and returns a new BrotliCompress object.

f
createBrotliDecompress

Creates and returns a new BrotliDecompress object.

f
createDeflate

Creates and returns a new Deflate object.

f
createDeflateRaw

Creates and returns a new DeflateRaw object.

f
createGunzip

Creates and returns a new Gunzip object.

f
createGzip

Creates and returns a new Gzip object.See example.

f
createInflate

Creates and returns a new Inflate object.

f
createInflateRaw

Creates and returns a new InflateRaw object.

f
createUnzip

Creates and returns a new Unzip object.

f
deflate
No documentation available
f
deflateRaw
No documentation available
f
deflateRawSync

Compress a chunk of data with DeflateRaw.

f
deflateSync

Compress a chunk of data with Deflate.

f
gunzip
No documentation available
f
gunzipSync

Decompress a chunk of data with Gunzip.

f
gzip
No documentation available
f
gzipSync

Compress a chunk of data with Gzip.

f
inflate
No documentation available
f
inflateRaw
No documentation available
f
inflateRawSync

Decompress a chunk of data with InflateRaw.

f
inflateSync

Decompress a chunk of data with Inflate.

f
unzip
No documentation available
f
unzipSync

Decompress a chunk of data with Unzip.

Interfaces

I
BrotliCompress
No documentation available
I
BrotliDecompress
No documentation available
I
Deflate
No documentation available
I
DeflateRaw
No documentation available
I
Gunzip
No documentation available
I
Gzip
No documentation available
I
Inflate
No documentation available
I
InflateRaw
No documentation available
I
Unzip
No documentation available
I
Zlib
No documentation available
I
ZlibParams
No documentation available
I
ZlibReset
No documentation available

Namespaces

N
constants
No documentation available

Type Aliases

T
CompressCallback
No documentation available
T
InputType
No documentation available

Variables

v
constants.BROTLI_DECODE
No documentation available
v
constants.BROTLI_DECODER_ERROR_UNREACHABLE
No documentation available
v
constants.BROTLI_DECODER_NEEDS_MORE_INPUT
No documentation available
v
constants.BROTLI_DECODER_NEEDS_MORE_OUTPUT
No documentation available
v
constants.BROTLI_DECODER_NO_ERROR
No documentation available
v
v
constants.BROTLI_DECODER_RESULT_ERROR
No documentation available
v
constants.BROTLI_DECODER_RESULT_SUCCESS
No documentation available
v
constants.BROTLI_DECODER_SUCCESS
No documentation available
v
constants.BROTLI_DEFAULT_MODE
No documentation available
v
constants.BROTLI_DEFAULT_QUALITY
No documentation available
v
constants.BROTLI_DEFAULT_WINDOW
No documentation available
v
constants.BROTLI_ENCODE
No documentation available
v
constants.BROTLI_LARGE_MAX_WINDOW_BITS
No documentation available
v
constants.BROTLI_MAX_INPUT_BLOCK_BITS
No documentation available
v
constants.BROTLI_MAX_QUALITY
No documentation available
v
constants.BROTLI_MAX_WINDOW_BITS
No documentation available
v
constants.BROTLI_MIN_INPUT_BLOCK_BITS
No documentation available
v
constants.BROTLI_MIN_QUALITY
No documentation available
v
constants.BROTLI_MIN_WINDOW_BITS
No documentation available
v
constants.BROTLI_MODE_FONT
No documentation available
v
constants.BROTLI_MODE_GENERIC
No documentation available
v
constants.BROTLI_MODE_TEXT
No documentation available
v
constants.BROTLI_OPERATION_EMIT_METADATA
No documentation available
v
constants.BROTLI_OPERATION_FINISH
No documentation available
v
constants.BROTLI_OPERATION_FLUSH
No documentation available
v
constants.BROTLI_OPERATION_PROCESS
No documentation available
v
constants.BROTLI_PARAM_LARGE_WINDOW
No documentation available
v
constants.BROTLI_PARAM_LGBLOCK
No documentation available
v
constants.BROTLI_PARAM_LGWIN
No documentation available
v
constants.BROTLI_PARAM_MODE
No documentation available
v
constants.BROTLI_PARAM_NDIRECT
No documentation available
v
constants.BROTLI_PARAM_NPOSTFIX
No documentation available
v
constants.BROTLI_PARAM_QUALITY
No documentation available
v
constants.BROTLI_PARAM_SIZE_HINT
No documentation available
v
constants.DEFLATE
No documentation available
v
constants.DEFLATERAW
No documentation available
v
constants.GUNZIP
No documentation available
v
constants.GZIP
No documentation available
v
constants.INFLATE
No documentation available
v
constants.INFLATERAW
No documentation available
v
constants.UNZIP
No documentation available
v
constants.Z_BEST_COMPRESSION
No documentation available
v
constants.Z_BEST_SPEED
No documentation available
v
constants.Z_BLOCK
No documentation available
v
constants.Z_BUF_ERROR
No documentation available
v
constants.Z_DATA_ERROR
No documentation available
v
constants.Z_DEFAULT_CHUNK
No documentation available
v
constants.Z_DEFAULT_COMPRESSION
No documentation available
v
constants.Z_DEFAULT_LEVEL
No documentation available
v
constants.Z_DEFAULT_MEMLEVEL
No documentation available
v
constants.Z_DEFAULT_STRATEGY
No documentation available
v
constants.Z_DEFAULT_WINDOWBITS
No documentation available
v
constants.Z_ERRNO
No documentation available
v
constants.Z_FILTERED
No documentation available
v
constants.Z_FINISH
No documentation available
v
constants.Z_FIXED
No documentation available
v
constants.Z_FULL_FLUSH
No documentation available
v
constants.Z_HUFFMAN_ONLY
No documentation available
v
constants.Z_MAX_CHUNK
No documentation available
v
constants.Z_MAX_LEVEL
No documentation available
v
constants.Z_MAX_MEMLEVEL
No documentation available
v
constants.Z_MAX_WINDOWBITS
No documentation available
v
constants.Z_MEM_ERROR
No documentation available
v
constants.Z_MIN_CHUNK
No documentation available
v
constants.Z_MIN_LEVEL
No documentation available
v
constants.Z_MIN_MEMLEVEL
No documentation available
v
constants.Z_MIN_WINDOWBITS
No documentation available
v
constants.Z_NEED_DICT
No documentation available
v
constants.Z_NO_COMPRESSION
No documentation available
v
constants.Z_NO_FLUSH
No documentation available
v
constants.Z_OK
No documentation available
v
constants.Z_PARTIAL_FLUSH
No documentation available
v
constants.Z_RLE
No documentation available
v
constants.Z_STREAM_END
No documentation available
v
constants.Z_STREAM_ERROR
No documentation available
v
constants.Z_SYNC_FLUSH
No documentation available
v
constants.Z_TREES
No documentation available
v
constants.Z_VERSION_ERROR
No documentation available
v
constants.ZLIB_VERNUM
No documentation available
v
Z_ASCII
No documentation available
v
Z_BEST_COMPRESSION
No documentation available
v
Z_BEST_SPEED
No documentation available
v
Z_BINARY
No documentation available
v
Z_BLOCK
No documentation available
v
Z_BUF_ERROR
No documentation available
v
Z_DATA_ERROR
No documentation available
v
Z_DEFAULT_COMPRESSION
No documentation available
v
Z_DEFAULT_STRATEGY
No documentation available
v
Z_DEFLATED
No documentation available
v
Z_ERRNO
No documentation available
v
Z_FILTERED
No documentation available
v
Z_FINISH
No documentation available
v
Z_FIXED
No documentation available
v
Z_FULL_FLUSH
No documentation available
v
Z_HUFFMAN_ONLY
No documentation available
v
Z_MEM_ERROR
No documentation available
v
Z_NEED_DICT
No documentation available
v
Z_NO_COMPRESSION
No documentation available
v
Z_NO_FLUSH
No documentation available
v
Z_OK
No documentation available
v
Z_PARTIAL_FLUSH
No documentation available
v
Z_RLE
No documentation available
v
Z_STREAM_END
No documentation available
v
Z_STREAM_ERROR
No documentation available
v
Z_SYNC_FLUSH
No documentation available
v
Z_TEXT
No documentation available
v
Z_TREES
No documentation available
v
Z_UNKNOWN
No documentation available
v
Z_VERSION_ERROR
No documentation available