Zlib bindings

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

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 parsed as JSON.

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

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

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).