Streaming Bzip2 Decompression

1.4.3 · maintenance · verified Sun Apr 19

unbzip2-stream provides a pure JavaScript implementation for decompressing bzip2 (bz2) files and streams, designed to work seamlessly in both Node.js environments and modern browsers (via browserify). The current stable version is 1.4.3, which was last published over six years ago, indicating a maintenance phase rather than active development. Despite its age, it remains a widely used solution for streaming bzip2 decompression due to its pure JavaScript nature, avoiding native dependencies that can complicate cross-platform deployment. It integrates with Node.js streams for efficient processing of large compressed data. A key differentiator is its ability to handle bzip2 streams entirely in JavaScript, distinguishing it from solutions that might rely on compiled binaries or system-level bzip2 tools. It solely focuses on decompression, not compression.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to decompress a bzip2 (.bz2) file from a `fs.ReadStream` and pipe the decompressed data to a `fs.WriteStream` using `unbzip2-stream` in Node.js.

const bz2 = require('unbzip2-stream');
const fs = require('fs');
const path = require('path');

// Create a dummy bzip2 file for demonstration (requires external bzip2 utility)
// In a real scenario, you'd have an existing .bz2 file.
// For this example, we'll simulate an input stream.

const inputFilePath = path.join(__dirname, 'test.bz2');
const outputFilePath = path.join(__dirname, 'test.txt');

// --- IMPORTANT: This part needs a real .bz2 file or a generator ---
// For a runnable example, you might create a simple bz2 file first:
// echo "Hello, world! This is a test string for bzip2 decompression." | bzip2 > test.bz2
// Assuming 'test.bz2' exists in the same directory

// Ensure output file is cleaned up if exists
if (fs.existsSync(outputFilePath)) {
  fs.unlinkSync(outputFilePath);
}

fs.createReadStream(inputFilePath)
  .pipe(bz2())
  .pipe(fs.createWriteStream(outputFilePath))
  .on('finish', () => {
    console.log(`Decompression complete: ${inputFilePath} -> ${outputFilePath}`);
    console.log('Output file content:');
    console.log(fs.readFileSync(outputFilePath, 'utf8'));
  })
  .on('error', (err) => {
    console.error('Decompression failed:', err);
  });

view raw JSON →