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; });
Compress a chunk of data with BrotliCompress
.
Decompress a chunk of data with BrotliDecompress
.
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.
Creates and returns a new BrotliCompress
object.
Creates and returns a new BrotliDecompress
object.
Creates and returns a new Deflate
object.
Creates and returns a new DeflateRaw
object.
Creates and returns a new Gunzip
object.
Creates and returns a new Gzip
object.See example
.
Creates and returns a new Inflate
object.
Creates and returns a new InflateRaw
object.
Creates and returns a new Unzip
object.
Compress a chunk of data with DeflateRaw
.
Compress a chunk of data with Deflate
.
Decompress a chunk of data with Gunzip
.
Compress a chunk of data with Gzip
.
Decompress a chunk of data with InflateRaw
.
Decompress a chunk of data with Inflate
.
Decompress a chunk of data with Unzip
.