Class: Omnizip::Algorithms::BZip2

Inherits:
Omnizip::Algorithm show all
Defined in:
lib/omnizip/algorithms/bzip2.rb,
lib/omnizip/algorithms/bzip2/bwt.rb,
lib/omnizip/algorithms/bzip2/mtf.rb,
lib/omnizip/algorithms/bzip2/rle.rb,
lib/omnizip/algorithms/bzip2/decoder.rb,
lib/omnizip/algorithms/bzip2/encoder.rb,
lib/omnizip/algorithms/bzip2/huffman.rb

Overview

BZip2 block-sorting compression algorithm

BZip2 combines several compression techniques in a pipeline:

  1. Burrows-Wheeler Transform (BWT) - block-sorting transformation
  2. Move-to-Front Transform (MTF) - exploits locality
  3. Run-Length Encoding (RLE) - compresses repeated bytes
  4. Huffman Coding - variable-length entropy encoding

This algorithm is particularly effective for:

  • Text files with repetitive patterns
  • Data with high local similarity
  • Files where block-sorting improves compressibility

Block size affects both compression ratio and memory usage. Larger blocks (up to 900KB) generally provide better compression but require more memory.

Defined Under Namespace

Classes: Bwt, Decoder, Encoder, Huffman, Mtf, Rle

Instance Attribute Summary

Attributes inherited from Omnizip::Algorithm

#filter, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Omnizip::Algorithm

inherited, #initialize, #with_filter

Constructor Details

This class inherits a constructor from Omnizip::Algorithm

Class Method Details

.metadataAlgorithmMetadata

Get algorithm metadata

Returns:

  • (AlgorithmMetadata)

    Algorithm information



56
57
58
59
60
61
62
63
# File 'lib/omnizip/algorithms/bzip2.rb', line 56

def self.
  Models::AlgorithmMetadata.new.tap do |meta|
    meta.name = "bzip2"
    meta.description = "BZip2 block-sorting compression using " \
                       "BWT, MTF, RLE, and Huffman coding"
    meta.version = "1.0.0"
  end
end

Instance Method Details

#compress(input_stream, output_stream, options = nil) ⇒ void

This method returns an undefined value.

Compress data using BZip2 algorithm

Parameters:

  • input_stream (IO)

    Input stream to compress

  • output_stream (IO)

    Output stream for compressed data

  • options (Models::CompressionOptions) (defaults to: nil)

    Compression options



71
72
73
74
75
76
# File 'lib/omnizip/algorithms/bzip2.rb', line 71

def compress(input_stream, output_stream, options = nil)
  input_data = input_stream.read
  encoder = Encoder.new(output_stream,
                        build_encoder_options(options))
  encoder.encode_stream(input_data)
end

#decompress(input_stream, output_stream, _options = nil) ⇒ void

This method returns an undefined value.

Decompress BZip2-compressed data

Parameters:

  • input_stream (IO)

    Input stream of compressed data

  • output_stream (IO)

    Output stream for decompressed data

  • options (Models::CompressionOptions)

    Decompression options



84
85
86
87
88
89
90
91
# File 'lib/omnizip/algorithms/bzip2.rb', line 84

def decompress(input_stream, output_stream, _options = nil)
  if output_stream.respond_to?(:set_encoding)
    output_stream.set_encoding(Encoding::BINARY)
  end
  decoder = Decoder.new(input_stream)
  decompressed = decoder.decode_stream
  output_stream.write(decompressed)
end