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 --saveThe 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.
constbzip2=require('node-bzip2');// Compress some dataconstcompressedBytes=bzip2.compress('Hello, world!',{level: 9,buffering: 'auto'});// Decompress the dataconstdecompressedBytes=bzip2.decompress(compressedBytes,{small: false});// Decode the decompressed data as a UTF-8 stringconstdecompressed=(newTextDecoder('utf8')).decode(decompressedBytes);console.log(decompressed);// Hello, world!You can also use the async functions to compress and decompress data asynchronously:
constbzip2=require('node-bzip2');// Compress some dataconstcompressedBytes=awaitbzip2.compressAsync('Hello, world!',{level: 9,buffering: 'auto'});// Decompress the dataconstdecompressedBytes=awaitbzip2.decompressAsync(compressedBytes,{small: false});// Decode the decompressed data as a UTF-8 stringconstdecompressed=(newTextDecoder('utf8')).decode(decompressedBytes);console.log(decompressed);// Hello, world!