Seekable Bzip2 Decoder

2.0.0 · abandoned · verified Sun Apr 19

seek-bzip is a pure-JavaScript Node.js module (version 2.0.0, last published over 6 years ago) designed for random-access decoding of bzip2 data. Its primary differentiator is the ability to seek to and decompress individual blocks within a bzip2 file, provided an external index. It mainly operates synchronously, decoding buffers into other buffers. While it offers experimental support for Node.js streams, this functionality relies on the `fibers` package, which is deprecated and largely incompatible with modern Node.js versions (v16+), severely limiting its utility in current environments. The package also includes a `seek-bzip-table` tool for generating the necessary block indices.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic synchronous buffer-to-buffer decompression using `Bunzip.decode`. It includes a warning that a real bzip2 file (not the dummy one created in the example) is required for successful operation.

const Bunzip = require('seek-bzip');
const fs = require('fs');
const path = require('path');

// IMPORTANT: This package decodes bzip2. To truly test, you would need
// a valid `example.bz2` file created by a bzip2 compressor (e.g., `bzip2 -c original.txt > example.bz2`).
// For demonstration, we'll create a dummy file, but Bunzip.decode will fail if it's not actual bzip2.

const dummyCompressedPath = path.join(__dirname, 'dummy_example.bz2');
const dummyOriginalData = Buffer.from('BZ2h1A&SY99999999999', 'ascii'); // Not actual bzip2 data
fs.writeFileSync(dummyCompressedPath, dummyOriginalData);

const decompressedOutputPath = path.join(__dirname, 'decompressed_output.txt');

try {
  console.log(`Attempting to read dummy bzip2 data from: ${dummyCompressedPath}`);
  const compressedData = fs.readFileSync(dummyCompressedPath);

  console.log('Attempting to decode...');
  // This will likely throw an error because dummyOriginalData is not valid bzip2
  const data = Bunzip.decode(compressedData);

  fs.writeFileSync(decompressedOutputPath, data);
  console.log(`Successfully (or not) decompressed data to: ${decompressedOutputPath}`);
  console.log(`Decoded data length: ${data.length}`);
} catch (error) {
  console.error('Error during decompression:', error.message);
  console.warn('\nNOTE: The `dummy_example.bz2` created in this quickstart is NOT a real bzip2 file.');
  console.warn('`seek-bzip` requires actual bzip2 compressed data for successful decoding.');
  console.warn('To test properly, manually create `example.bz2` using a bzip2 compressor, then replace `dummyCompressedPath` with `path.join(__dirname, 'example.bz2')`.');
} finally {
  if (fs.existsSync(dummyCompressedPath)) {
    fs.unlinkSync(dummyCompressedPath);
  }
  // You might want to keep decompressed_output.txt for inspection if it ever succeeds.
  // if (fs.existsSync(decompressedOutputPath)) {
  //   fs.unlinkSync(decompressedOutputPath);
  // }
}

view raw JSON →