Zlib bindings

The zlib module provides single-call and stream-oriented functions for interacting with zlib data.

Classes

zlib.zstrmd

Represents a handle for interacting with a deflate stream initiated by defnew().

zlib.zstrmi

Represents a handle for interacting with an inflate stream initiated by infnew().

Methods

deflate(str_or_resource, gzipopt, nullable, levelopt, nullable) → {string}nullable

Compresses data in Zlib or gzip format.

If the input argument is a plain string, it is directly compressed.

If an array, object or resource value is given, this function will attempt to invoke a read() method on it to read chunks of input text to incrementally compress. Reading will stop if the object's read() method returns either null or an empty string.

Throws an exception on errors.

Returns the compressed data.

Parameters:
NameTypeDescription
str_or_resourcestring

The string or resource object to be compressed.

gzipboolean(optional, nullable, default: false)

Add a gzip header if true (creates a gzip-compliant output, otherwise defaults to Zlib)

levelnumber(optional, nullable, default: Z_DEFAULT_COMPRESSION)

The compression level (0-9).

Returns: string
Example
// deflate content using default compression
const deflated = deflate(content);

// deflate content using fastest compression
const deflated = deflate(content, Z_BEST_SPEED);

defnew(gzipopt, nullable, levelopt, nullable) → {zstrmd}nullable

Initializes a deflate stream.

Returns a stream handle on success.

Returns null if an error occurred.

Parameters:
NameTypeDescription
gzipboolean(optional, nullable, default: false)

Add a gzip header if true (creates a gzip-compliant output, otherwise defaults to Zlib)

levelnumber(optional, nullable, default: Z_DEFAULT_COMPRESSION)

The compression level (0-9).

Returns: zstrmd
Example
// initialize a Zlib deflate stream using default compression
const zstrmd = defnew();

// initialize a gzip deflate stream using fastest compression
const zstrmd = defnew(true, Z_BEST_SPEED);

inflate(str_or_resource) → {string}nullable

Decompresses data in Zlib or gzip format.

If the input argument is a plain string, it is directly decompressed.

If an array, object or resource value is given, this function will attempt to invoke a read() method on it to read chunks of input text to incrementally decompress. Reading will stop if the object's read() method returns either null or an empty string.

Throws an exception on errors.

Returns the decompressed data.

Parameters:
NameTypeDescription
str_or_resourcestring

The string or resource object to be parsed as JSON.

Returns: string

infnew() → {zstrmi}nullable

Initializes an inflate stream. Can process either Zlib or gzip data.

Returns a stream handle on success.

Returns null if an error occurred.

Returns: zstrmi
Example
// initialize an inflate stream
const zstrmi = infnew();

Type Definitions

Compression levels

Constants representing predefined compression levels.

Properties
NameTypeDescription
Z_NO_COMPRESSION.number
Z_BEST_SPEED.number
Z_BEST_COMPRESSION.number
Z_DEFAULT_COMPRESSIONnumber

default compromise between speed and compression (currently equivalent to level 6).

flush options

Constants representing flush options.

Properties
NameTypeDescription
Z_NO_FLUSH.number
Z_PARTIAL_FLUSH.number
Z_SYNC_FLUSH.number
Z_FULL_FLUSH.number
Z_FINISH.number