streaming bzip2 decompressor in pure JS for Node and browsers.
In both environments, the library uses TransformStream
s and Uint8Array
s.
import unbzip2Stream from 'unbzip2-stream';
// decompress test.bz2 and output the result
const response = await fetch('./test.bz2');
const decompressedStream = unbzip2Stream(response.data);
for await(const chunk of decompressedStream) {
console.log(chunk);
}
const unbzip2Stream = require('unbzip2-stream');
const fs = require('fs');
// decompress test.bz2 and output the result
let stream = fs.createReadStream('./test.bz2');
stream = stream.Readable.toWeb(stream);
stream = unbzip2Stream(stream);
stream = stream.Readable.fromWeb(stream);
stream.pipe(process.stdout);
Also see test/browser/download.js for a complete example of decompressing a file while downloading.
To run tests in Node:
npm run test
To run tests in PhantomJS
npm run browser-test
There are two more tests that specifically test decompression of a very large file. Because I don't want to include large binary files in this repository, the files are created by running an npm script.
npm run prepare-long-test
You can now
npm run long-test
And to run a test in chrome that downloads and decompresses a large binary file
npm run download-test
Open the browser's console to see the output.