NodeJS bindings for BZip2 (libbz2).
This package will compile the BZip2 library from source and link against it, exposing functions for compressing and decompressing data using the BZip2 algorithm in NodeJS.
This package does not work on the web and is designed for use in NodeJS only.
npm install node-bzip2 --save
The package exposes two functions: compress
and decompress
(and their respective async versions compressAsync
and decompressAsync
).
Both functions can take a string
, Buffer
, or typed array as input and return a Buffer
containing the compressed or decompressed data.
Additional options such as compression level and buffering behavior can be passed as an optional second argument, explained in the respective functions' JSDocs.
const bzip2 = require('node-bzip2');
// Compress some data
const compressedBytes = bzip2.compress('Hello, world!', { level: 9, buffering: 'auto' });
// Decompress the data
const decompressedBytes = bzip2.decompress(compressedBytes, { small: false });
// Decode the decompressed data as a UTF-8 string
const decompressed = (new TextDecoder('utf8')).decode(decompressedBytes);
console.log(decompressed); // Hello, world!
You can also use the async functions to compress and decompress data asynchronously:
const bzip2 = require('node-bzip2');
// Compress some data
const compressedBytes = await bzip2.compressAsync('Hello, world!', { level: 9, buffering: 'auto' });
// Decompress the data
const decompressedBytes = await bzip2.decompressAsync(compressedBytes, { small: false });
// Decode the decompressed data as a UTF-8 string
const decompressed = (new TextDecoder('utf8')).decode(decompressedBytes);
console.log(decompressed); // Hello, world!