packkit/bzip2

bzip2 codec — pure Gleam decoder for .bz2 streams.

The decoder consumes the "BZh" magic, a single-character block-size label (1..9), one or more compressed blocks, and the stream end-marker plus 32-bit combined CRC. Each block is reversed through Huffman decoding, MTF inversion, inverse Burrows-Wheeler, and the RLE1 expansion that bzip2 applies to the raw input before transformation. The encoder is intentionally deferred.

Values

pub fn codec() -> codec.Codec

bzip2 codec smart constructor.

pub fn decode(
  bytes bytes: BitArray,
) -> Result(BitArray, error.CodecError)

Decode a bzip2 stream using the shared default Limits.

pub fn decode_with_limits(
  bytes bytes: BitArray,
  limits limits: limit.Limits,
) -> Result(BitArray, error.CodecError)

Decode a bzip2 stream using explicit Limits.

Handles multi-stream .bz2 files (the bzcat-style concatenation of independent bzip2 streams). When the end-of-stream marker for one stream is reached, the decoder aligns to the next byte boundary and looks for another "BZh" magic; if present, the next stream’s payload is appended to the accumulated output.

pub fn encode(
  bytes bytes: BitArray,
) -> Result(BitArray, error.CodecError)

Encode bytes as a bzip2 stream using the default block size 9 (900 KiB). The encoder emits a single block plus the stream trailer with the combined CRC; large inputs that exceed the block size will still be packed into one block, so a naive forward BWT may dominate the running time.

pub fn encode_with_level(
  bytes bytes: BitArray,
  level level: Int,
) -> Result(BitArray, error.CodecError)

Encode bytes as a bzip2 stream with an explicit block-size level (1..9). The level only affects the stream header byte; the encoder always emits a single block so the level mostly carries over for round-trip tooling.

Search Document