zlib port to javascript - fast, modularized, with browser support
npx @tessl/cli install tessl/npm-pako@2.1.0Pako is a high-performance JavaScript port of the zlib compression library, enabling developers to perform deflate/inflate compression and decompression operations directly in JavaScript environments including browsers and Node.js. It offers binary-compatible results with the original zlib library while achieving near-native performance in modern JavaScript engines.
npm install pakoimport pako from 'pako';
import { deflate, inflate, Deflate, Inflate, constants } from 'pako';For CommonJS:
const pako = require('pako');
const { deflate, inflate, Deflate, Inflate, constants } = require('pako');import { deflate, inflate } from 'pako';
// Compress string data (automatically converted to UTF-8 bytes)
const input = "Hello World!";
const compressed = deflate(input);
// Decompress back to original
const decompressed = inflate(compressed, { to: 'string' });
console.log(decompressed); // "Hello World!"
// Working with binary data
const binaryData = new Uint8Array([1, 2, 3, 4, 5]);
const compressedBinary = deflate(binaryData);
const decompressedBinary = inflate(compressedBinary);
// All input types supported: string, Uint8Array, ArrayBuffer
const arrayBuffer = new ArrayBuffer(10);
const compressedFromBuffer = deflate(arrayBuffer);Pako is built around several key components:
deflate, inflate, gzip, ungzip)Deflate, Inflate) for processing large data chunksPako automatically handles different input types:
Input Types Supported:
string - Automatically converted to UTF-8 bytes using TextEncoder when available, or manual UTF-8 encodingUint8Array - Used directly as binary dataArrayBuffer - Converted to Uint8Array for processingOutput Types:
Uint8Array for all compression functions and binary decompression{ to: 'string' } option is specifiedHigh-performance data compression using deflate algorithm with support for raw deflate, standard deflate with wrapper, and gzip formats.
function deflate(input: Uint8Array | ArrayBuffer | string, options?: DeflateOptions): Uint8Array;
function deflateRaw(input: Uint8Array | ArrayBuffer | string, options?: DeflateOptions): Uint8Array;
function gzip(input: Uint8Array | ArrayBuffer | string, options?: DeflateOptions): Uint8Array;
class Deflate {
constructor(options?: DeflateOptions);
push(data: Uint8Array | ArrayBuffer | string, flush_mode?: number | boolean): boolean;
result: Uint8Array;
err: number;
msg: string;
chunks: Array<Uint8Array>;
ended: boolean;
options: DeflateOptions;
strm: any;
}Fast data decompression with automatic format detection for deflate and gzip formats, supporting both binary and string output.
function inflate(input: Uint8Array | ArrayBuffer, options?: InflateOptions): Uint8Array | string;
function inflateRaw(input: Uint8Array | ArrayBuffer, options?: InflateOptions): Uint8Array | string;
function ungzip(input: Uint8Array | ArrayBuffer, options?: InflateOptions): Uint8Array | string;
class Inflate {
constructor(options?: InflateOptions);
push(data: Uint8Array | ArrayBuffer, flush_mode?: number | boolean): boolean;
result: Uint8Array | string;
err: number;
msg: string;
chunks: Array<Uint8Array>;
ended: boolean;
options: InflateOptions;
strm: any;
header: any;
}interface DeflateOptions {
level?: number; // Compression level (-1 for default, 0-9)
windowBits?: number; // Window size (15 default, 8-15 range)
memLevel?: number; // Memory usage level (8 default, 1-9 range)
strategy?: number; // Compression strategy (0 default)
dictionary?: Uint8Array | ArrayBuffer | string; // Preset dictionary
chunkSize?: number; // Output chunk size (16384 default)
raw?: boolean; // Raw deflate without wrapper
gzip?: boolean; // Create gzip wrapper
header?: GzipHeader; // Custom gzip header
}
interface InflateOptions {
windowBits?: number; // Window size (15 default, 8-15 range)
dictionary?: Uint8Array | ArrayBuffer | string; // Preset dictionary
chunkSize?: number; // Output chunk size (65536 default)
raw?: boolean; // Raw inflate without wrapper
to?: string; // Output format ('string' for UTF-16, default binary)
}
interface GzipHeader {
text?: boolean; // Compressed data is text
time?: number; // Unix timestamp
os?: number; // Operating system code
extra?: Array<number>; // Extra data bytes (max 65536)
name?: string; // File name
comment?: string; // Comment
hcrc?: boolean; // Add header CRC
}const constants: {
// Flush values
Z_NO_FLUSH: 0;
Z_PARTIAL_FLUSH: 1;
Z_SYNC_FLUSH: 2;
Z_FULL_FLUSH: 3;
Z_FINISH: 4;
Z_BLOCK: 5;
Z_TREES: 6;
// Return codes
Z_OK: 0;
Z_STREAM_END: 1;
Z_NEED_DICT: 2;
Z_ERRNO: -1;
Z_STREAM_ERROR: -2;
Z_DATA_ERROR: -3;
Z_MEM_ERROR: -4;
Z_BUF_ERROR: -5;
// Compression levels
Z_NO_COMPRESSION: 0;
Z_BEST_SPEED: 1;
Z_BEST_COMPRESSION: 9;
Z_DEFAULT_COMPRESSION: -1;
// Compression strategies
Z_FILTERED: 1;
Z_HUFFMAN_ONLY: 2;
Z_RLE: 3;
Z_FIXED: 4;
Z_DEFAULT_STRATEGY: 0;
// Data types
Z_BINARY: 0;
Z_TEXT: 1;
Z_UNKNOWN: 2;
// Compression method
Z_DEFLATED: 8;
};