Zlib bindings
The zlib
module provides single-call and stream-oriented functions for interacting with zlib data.
- Source
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.
Name | Type | Description |
---|---|---|
str_or_resource | string | The string or resource object to be compressed. |
gzip | boolean | (optional, nullable, default: false )Add a gzip header if true (creates a gzip-compliant output, otherwise defaults to Zlib) |
level | number | (optional, nullable, default: Z_DEFAULT_COMPRESSION )The compression level (0-9). |
// deflate content using default compression
const deflated = deflate(content);
// deflate content using fastest compression
const deflated = deflate(content, Z_BEST_SPEED);
- Source
defnew(gzipopt, nullable, levelopt, nullable) → {zstrmd}nullable
Initializes a deflate stream.
Returns a stream handle on success.
Returns null
if an error occurred.
Name | Type | Description |
---|---|---|
gzip | boolean | (optional, nullable, default: false )Add a gzip header if true (creates a gzip-compliant output, otherwise defaults to Zlib) |
level | number | (optional, nullable, default: Z_DEFAULT_COMPRESSION )The compression level (0-9). |
// 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);
- Source
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.
Name | Type | Description |
---|---|---|
str_or_resource | string | The string or resource object to be parsed as JSON. |
- Source
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.
// initialize an inflate stream
const zstrmi = infnew();
- Source
Type Definitions
Compression levels
Constants representing predefined compression levels.
Name | Type | Description |
---|---|---|
Z_NO_COMPRESSION. | number | |
Z_BEST_SPEED. | number | |
Z_BEST_COMPRESSION. | number | |
Z_DEFAULT_COMPRESSION | number | default compromise between speed and compression (currently equivalent to level 6). |
- Source
flush options
Constants representing flush options.
Name | Type | Description |
---|---|---|
Z_NO_FLUSH. | number | |
Z_PARTIAL_FLUSH. | number | |
Z_SYNC_FLUSH. | number | |
Z_FULL_FLUSH. | number | |
Z_FINISH. | number |
- Source