zlib.js: JavaScript Compression Library

0.3.1 · abandoned · verified Sun Apr 19

zlib.js is a pure JavaScript implementation of various data compression algorithms, including ZLIB (RFC1950), DEFLATE (RFC1951), GZIP (RFC1952), and PKZIP. The current stable version is 0.3.1, released in July 2017. As a pure JavaScript solution, it provides cross-environment compatibility (Node.js and browser) without native dependencies. Its key differentiator lies in offering fine-grained control over different compression types and options directly within JavaScript, contrasting with environments like Node.js that provide native bindings to zlib. However, the package has not seen updates since 2017, suggesting it is no longer actively maintained and may lack modern features, performance optimizations, or security patches compared to contemporary alternatives.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates ZLIB compression and decompression, along with a basic GZIP compression example, using the Node.js entry point of zlib.js. It highlights how to initialize Deflate, Inflate, Gzip, and Gunzip classes, handle data as Uint8Array, and convert between strings and byte arrays.

import { Buffer } from 'buffer';

const Zlib = require('zlibjs/bin/node-zlib.js');

function stringToByteArray(str: string): Uint8Array {
    const array = new Uint8Array(str.length);
    for (let i = 0; i < str.length; ++i) {
        array[i] = str.charCodeAt(i) & 0xff;
    }
    return array;
}

const originalString = 'Hello, zlib.js! This is a test string to demonstrate compression and decompression capabilities.';
const plainData = stringToByteArray(originalString);

console.log('Original data length:', plainData.length);

// Compress using ZLIB Deflate
const deflate = new Zlib.Deflate(plainData, {
  compressionType: Zlib.Deflate.CompressionType.DYNAMIC
});
const compressed = deflate.compress();

console.log('Compressed data length (Deflate):', compressed.length);

// Decompress using ZLIB Inflate
const inflate = new Zlib.Inflate(compressed);
const decompressed = inflate.decompress();

console.log('Decompressed data length (Inflate):', decompressed.length);

const decompressedString = Buffer.from(decompressed).toString('utf8');
console.log('Decompressed string:', decompressedString);

if (originalString === decompressedString) {
  console.log('Compression and decompression successful!');
} else {
  console.error('Data mismatch after compression/decompression.');
}

// GZIP Compression example
const gzip = new Zlib.Gzip(plainData, {
    flags: { fname: true, comment: false, fhcrc: false },
    filename: stringToByteArray('test.txt')
});
const gzipped = gzip.compress();
console.log('GZIP compressed data length:', gzipped.length);

// GZIP Decompression example
const gunzip = new Zlib.Gunzip(gzipped);
const gunzipped = gunzip.decompress();
console.log('GZIP decompressed data length:', gunzipped.length);
const gunzippedString = Buffer.from(gunzipped).toString('utf8');
console.log('GZIP decompressed string:', gunzippedString);

view raw JSON →