zlib
The zlib module provides Node.js-style compression and decompression APIs for JSH applications.
It supports gzip, deflate, raw deflate, auto-detected unzip, synchronous helpers, callback-based asynchronous helpers, and stream-style processing.
Typical usage looks like this.
const zlib = require('zlib');Synchronous methods
These methods return an ArrayBuffer.
gzipSync()
Compresses data using gzip.
gzipSync(data)gunzipSync()
Decompresses gzip data.
gunzipSync(data)deflateSync()
Compresses data using deflate.
deflateSync(data)inflateSync()
Decompresses deflate data.
inflateSync(data)deflateRawSync()
Compresses data using raw deflate.
deflateRawSync(data)inflateRawSync()
Decompresses raw deflate data.
inflateRawSync(data)unzipSync()
Decompresses gzip or deflate data using automatic format detection.
unzipSync(data)Input type
Compression methods accept String or binary input.
Decompression methods expect compressed binary input.
Usage example
| |
Asynchronous methods
These methods are callback-based.
gzip(), gunzip(), deflate(), inflate(), deflateRaw(), inflateRaw(), unzip()
Syntax
gzip(data, callback)
gunzip(data, callback)
deflate(data, callback)
inflate(data, callback)
deflateRaw(data, callback)
inflateRaw(data, callback)
unzip(data, callback)The callback signature is:
(err, result) => {}result is returned as an ArrayBuffer.
Usage example
| |
Stream factory methods
The module also provides stream-style compression and decompression objects.
createGzip()createGunzip()createDeflate()createInflate()createDeflateRaw()createInflateRaw()createUnzip()
Each factory returns a zlib stream object with these members.
| Member | Description |
|---|---|
write(data) | Writes input data into the stream. |
end([data]) | Optionally writes one final chunk and finishes the stream. |
on(event, callback) | Registers a listener for data, end, or error. |
pipe(dest[, options]) | Pipes stream output to another writable destination. |
flush() | Flushes pending compression output when supported. |
close() | Closes the underlying compression/decompression object. |
bytesWritten | Number of input bytes accepted so far. |
bytesRead | Number of output bytes produced so far. |
Streaming example
| |
pipe()
pipe() supports:
- JavaScript writable destinations with
write(chunk)and optionalend() - native writer-backed objects exposed as
writer
By default, pipe() calls the destination’s end() when the zlib stream ends.
You can disable that behavior with { end: false }.
| |
Progress tracking
Streaming objects expose running byte counters.
bytesWritten: total input bytes consumedbytesRead: total output bytes produced
These counters are updated while data flows, so they can be checked inside data callbacks.
| |
constants
The module exports zlib constants as zlib.constants.
Representative values include:
- flush constants such as
Z_NO_FLUSH,Z_SYNC_FLUSH,Z_FINISH - compression levels such as
Z_NO_COMPRESSION,Z_BEST_SPEED,Z_BEST_COMPRESSION,Z_DEFAULT_COMPRESSION - return/status constants such as
Z_OK,Z_STREAM_END,Z_DATA_ERROR
| |
Compatibility notes
- The API shape is Node.js-like, but it is not a full drop-in replacement for Node.js
zlib. - Stream
on()supportsdata,end, anderrorcallbacks only. - Each zlib stream stores one callback per event type; later
on()calls for the same event replace the previous callback. - Async helpers are callback-based only; promise-based variants are not provided.