What is unbzip2-stream?
The unbzip2-stream npm package is a Node.js library used for decompressing data in the bzip2 format. It is particularly useful for handling large bzip2 files or streams, such as those downloaded from the internet or read from a file system, by decompressing them on-the-fly.
What are unbzip2-stream's main functionalities?
Decompress bzip2 file
This code demonstrates how to decompress a bzip2 compressed file using unbzip2-stream. It reads a bzip2 file, decompresses it, and writes the decompressed data to a new file.
const fs = require('fs');
const bunzip2 = require('unbzip2-stream');
fs.createReadStream('path/to/compressed-file.bz2')
.pipe(bunzip2())
.pipe(fs.createWriteStream('path/to/decompressed-file'));
Decompress bzip2 stream
This example shows how to decompress a bzip2 stream from a URL and pipe the output directly to the standard output. This is useful for processing data on-the-fly without storing it.
const request = require('request');
const bunzip2 = require('unbzip2-stream');
request('http://example.com/file.bz2')
.pipe(bunzip2())
.pipe(process.stdout);
Other packages similar to unbzip2-stream
node-bzip
node-bzip is another npm package that provides bzip2 compression and decompression functionalities. Compared to unbzip2-stream, node-bzip offers both compression and decompression, whereas unbzip2-stream is focused only on decompression.
compressjs
compressjs is a package that includes bzip2 compression and decompression among other compression algorithms. It provides a broader range of compression algorithms than unbzip2-stream, making it more versatile if multiple compression methods are needed.
unzip2-stream
streaming bzip2 compresser in pure JS for Node and browserify.
Wraps jvrousseau/bzip2.js into a through stream. I refactored the pre-existing streaming code (formerly in node-test.js) and turned it into the module's interface.
Buffers
When browserified, the stream emits instances of feross/buffer instead of raw Uint8Arrays to have a consistant API across browsers and Node.
Usage
var bz2 = require('unbzip2-stream');
var fs = require('fs');
fs.createReadStream('./test.bz2').pipe(bz2()).pipe(process.stdout);
Also see test/browser/download.js for an example of decompressing a file while downloading.
Tests
To run tests in Node:
npm run test
To run tests in PhantomJS
npm run browser-test
To run a test in chrome (if installed) that downloads and decompresses a linux kernel
(open the browser's console to see the output)
npm run download-test