{"id":12707,"library":"zlibjs","title":"zlib.js: JavaScript Compression Library","description":"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.","status":"abandoned","version":"0.3.1","language":"javascript","source_language":"en","source_url":"git://github.com/imaya/zlib.js","tags":["javascript"],"install":[{"cmd":"npm install zlibjs","lang":"bash","label":"npm"},{"cmd":"yarn add zlibjs","lang":"bash","label":"yarn"},{"cmd":"pnpm add zlibjs","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"For Node.js, the 'node-zlib.js' entry point is required, and it exports a single 'Zlib' object containing all compression/decompression classes. Browser usage typically involves loading a .min.js file which exposes 'Zlib' globally.","wrong":"import Zlib from 'zlibjs';","symbol":"Zlib","correct":"const Zlib = require('zlibjs/bin/node-zlib.js');"},{"note":"The 'Deflate' class is accessed as a property of the main 'Zlib' object loaded via CommonJS, not as a direct named export from the package.","wrong":"import { Deflate } from 'zlibjs';","symbol":"Deflate","correct":"const Zlib = require('zlibjs/bin/node-zlib.js'); const compressor = new Zlib.Deflate(plainData);"},{"note":"Similar to 'Deflate', the 'Inflate' class is a property of the 'Zlib' object and not directly importable via ESM syntax, which is not supported by this package anyway.","wrong":"import { Inflate } from 'zlibjs/bin/node-zlib.js';","symbol":"Inflate","correct":"const Zlib = require('zlibjs/bin/node-zlib.js'); const decompressor = new Zlib.Inflate(compressedData);"}],"quickstart":{"code":"import { Buffer } from 'buffer';\n\nconst Zlib = require('zlibjs/bin/node-zlib.js');\n\nfunction stringToByteArray(str: string): Uint8Array {\n    const array = new Uint8Array(str.length);\n    for (let i = 0; i < str.length; ++i) {\n        array[i] = str.charCodeAt(i) & 0xff;\n    }\n    return array;\n}\n\nconst originalString = 'Hello, zlib.js! This is a test string to demonstrate compression and decompression capabilities.';\nconst plainData = stringToByteArray(originalString);\n\nconsole.log('Original data length:', plainData.length);\n\n// Compress using ZLIB Deflate\nconst deflate = new Zlib.Deflate(plainData, {\n  compressionType: Zlib.Deflate.CompressionType.DYNAMIC\n});\nconst compressed = deflate.compress();\n\nconsole.log('Compressed data length (Deflate):', compressed.length);\n\n// Decompress using ZLIB Inflate\nconst inflate = new Zlib.Inflate(compressed);\nconst decompressed = inflate.decompress();\n\nconsole.log('Decompressed data length (Inflate):', decompressed.length);\n\nconst decompressedString = Buffer.from(decompressed).toString('utf8');\nconsole.log('Decompressed string:', decompressedString);\n\nif (originalString === decompressedString) {\n  console.log('Compression and decompression successful!');\n} else {\n  console.error('Data mismatch after compression/decompression.');\n}\n\n// GZIP Compression example\nconst gzip = new Zlib.Gzip(plainData, {\n    flags: { fname: true, comment: false, fhcrc: false },\n    filename: stringToByteArray('test.txt')\n});\nconst gzipped = gzip.compress();\nconsole.log('GZIP compressed data length:', gzipped.length);\n\n// GZIP Decompression example\nconst gunzip = new Zlib.Gunzip(gzipped);\nconst gunzipped = gunzip.decompress();\nconsole.log('GZIP decompressed data length:', gunzipped.length);\nconst gunzippedString = Buffer.from(gunzipped).toString('utf8');\nconsole.log('GZIP decompressed string:', gunzippedString);","lang":"typescript","description":"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."},"warnings":[{"fix":"Consider migrating to actively maintained libraries like `pako` for browser/universal use or Node.js's built-in `zlib` module for server-side applications, which offer better performance, security, and modern APIs.","message":"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.","severity":"breaking","affected_versions":">=0.3.1"},{"fix":"Always validate GZIP compressed output with other tools and consider alternatives if full GZIP specification compliance is critical.","message":"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.","severity":"gotcha","affected_versions":">=0.3.0"},{"fix":"Use CommonJS `require()` syntax directly. For browser environments, ensure the correct minified script is loaded globally. Modern bundlers might require specific configurations (e.g., aliasing or CJS compatibility loaders) to integrate it.","message":"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.","severity":"gotcha","affected_versions":">=0.3.0"},{"fix":"Avoid using the `lazy` option and rely on default or other configuration parameters for compression settings.","message":"The `lazy` matching parameter in `Zlib.Deflate` options is explicitly marked as deprecated in the README documentation.","severity":"deprecated","affected_versions":">=0.3.0"},{"fix":"Always convert string data to `Uint8Array` before passing it to compression functions, and convert decompressed `Uint8Array` back to strings as needed. Helper functions like `stringToByteArray` are often necessary.","message":"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.","severity":"gotcha","affected_versions":">=0.3.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure 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.","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.","error":"TypeError: Zlib.Deflate is not a constructor"},{"fix":"In 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.","cause":"The Zlib object was not imported or included in the environment.","error":"ReferenceError: Zlib is not defined"},{"fix":"This package does not officially support ESM. Use CommonJS `require()` syntax instead: `const Zlib = require('zlibjs/bin/node-zlib.js');`.","cause":"Attempting to use ES module `import` syntax in a CommonJS-only Node.js environment or without proper bundler configuration.","error":"SyntaxError: Cannot use import statement outside a module"}],"ecosystem":"npm"}