zlib.js: JavaScript Compression Library
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
-
TypeError: Zlib.Deflate is not a constructor
cause The Zlib object was not correctly imported or is not available in the current scope. This typically happens if the CommonJS `require` path is wrong or if using ESM `import` syntax.fixEnsure you are using `const Zlib = require('zlibjs/bin/node-zlib.js');` for Node.js environments. For browsers, ensure `zlib_and_gzip.min.js` or similar files are loaded globally before usage. -
ReferenceError: Zlib is not defined
cause The Zlib object was not imported or included in the environment.fixIn Node.js, ensure `require('zlibjs/bin/node-zlib.js')` is called and assigned to a `Zlib` variable. In browsers, verify that the appropriate `zlib.js` distribution file is included via a `<script>` tag before your application code. -
SyntaxError: Cannot use import statement outside a module
cause Attempting to use ES module `import` syntax in a CommonJS-only Node.js environment or without proper bundler configuration.fixThis package does not officially support ESM. Use CommonJS `require()` syntax instead: `const Zlib = require('zlibjs/bin/node-zlib.js');`.
Warnings
- breaking This package is no longer actively maintained, with the last release in July 2017. It may contain unpatched security vulnerabilities, compatibility issues with newer Node.js versions or browser environments, and lacks modern features or performance optimizations.
- gotcha The GZIP implementation in zlib.js is explicitly stated as 'incomplete' in the README, though it claims to be sufficient for 'usual use'. Developers should test thoroughly for edge cases or specific GZIP features they require.
- gotcha The package primarily targets CommonJS for Node.js via a specific path ('zlibjs/bin/node-zlib.js') and global 'Zlib' for browsers. It does not provide official ESM exports, requiring manual adaptation for modern module bundlers or native ESM environments.
- deprecated The `lazy` matching parameter in `Zlib.Deflate` options is explicitly marked as deprecated in the README documentation.
- gotcha zlib.js uses `Array.<number>` or `Uint8Array` for all input and output data. Incorrectly passing JavaScript strings or other data types will lead to runtime errors or corrupted compression.
Install
-
npm install zlibjs -
yarn add zlibjs -
pnpm add zlibjs
Imports
- Zlib
import Zlib from 'zlibjs';
const Zlib = require('zlibjs/bin/node-zlib.js'); - Deflate
import { Deflate } from 'zlibjs';const Zlib = require('zlibjs/bin/node-zlib.js'); const compressor = new Zlib.Deflate(plainData); - Inflate
import { Inflate } from 'zlibjs/bin/node-zlib.js';const Zlib = require('zlibjs/bin/node-zlib.js'); const decompressor = new Zlib.Inflate(compressedData);
Quickstart
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);