Class: Omnizip::Algorithms::LZMA::Encoder

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/omnizip/algorithms/lzma/encoder.rb

Overview

LZMA Encoder - Factory for LZMA compression implementations

This class provides a unified interface for LZMA encoding, delegating to the appropriate implementation based on the target format:

  1. SDK-compatible (default): For 7-Zip containers, uses 7-Zip SDK implementation
  2. XZ-compatible: For XZ/LZMA files, uses XZ Utils implementation

The encoder produces a stream that consists of:

  • Property byte (lc, lp, pb parameters)
  • Dictionary size (4 bytes)
  • Uncompressed size (8 bytes)
  • Compressed data

Constant Summary

Constants included from Constants

Constants::BIT_MODEL_TOTAL, Constants::COMPRESSION_LEVEL_DEFAULT, Constants::COMPRESSION_LEVEL_MAX, Constants::COMPRESSION_LEVEL_MIN, Constants::DICT_SIZE_MAX, Constants::DICT_SIZE_MIN, Constants::DIST_ALIGN_BITS, Constants::DIST_ALIGN_SIZE, Constants::DIST_SLOT_FAST_LIMIT, Constants::END_POS_MODEL_INDEX, Constants::EOS_MARKER, Constants::INIT_PROBS, Constants::LEN_HIGH_SYMBOLS, Constants::LEN_LOW_SYMBOLS, Constants::LEN_MID_SYMBOLS, Constants::LIT_SIZE_MAX, Constants::MATCH_LEN_MAX, Constants::MATCH_LEN_MIN, Constants::MOVE_BITS, Constants::NUM_DIRECT_BITS, Constants::NUM_DIST_SLOTS, Constants::NUM_DIST_SLOT_BITS, Constants::NUM_FULL_DISTANCES, Constants::NUM_LEN_HIGH_BITS, Constants::NUM_LEN_LOW_BITS, Constants::NUM_LEN_MID_BITS, Constants::NUM_LEN_TO_POS_STATES, Constants::NUM_LIT_CONTEXT_BITS_MAX, Constants::NUM_LIT_POS_BITS_MAX, Constants::NUM_POS_BITS_MAX, Constants::NUM_STATES, Constants::POS_STATES_MAX, Constants::START_POS_MODEL_INDEX, Constants::TOP

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, options = {}) ⇒ Encoder

Initialize the encoder

Parameters:

  • output (IO)

    Output stream for compressed data

  • options (Hash) (defaults to: {})

    Encoding options

Options Hash (options):

  • :dict_size (Integer)

    Dictionary size

  • :lc (Integer)

    Literal context bits (0-8)

  • :lp (Integer)

    Literal position bits (0-4)

  • :pb (Integer)

    Position bits (0-4)

  • :write_size (Boolean)

    Write actual size (false for standalone .lzma)

  • :sdk_compatible (Boolean)

    Use SDK-compatible encoding (default: true)

  • :xz_compatible (Boolean)

    Use XZ-compatible encoding (default: false)

  • :raw_mode (Boolean)

    Skip header for raw LZMA encoding (for 7-Zip/LZMA2)



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/omnizip/algorithms/lzma/encoder.rb', line 56

def initialize(output, options = {})
  @output = output
  @dict_size = options[:dict_size] || (1 << 16) # 64KB default
  @lc = options[:lc] || 3
  @lp = options[:lp] || 0
  @pb = options[:pb] || 2
  @write_size = options.fetch(:write_size, true)
  @xz_compatible = options.fetch(:xz_compatible, false)
  @sdk_compatible = options.fetch(:sdk_compatible, !@xz_compatible)
  @raw_mode = options.fetch(:raw_mode, false)

  validate_parameters

  # Factory pattern: create implementation based on mode
  @impl = if @xz_compatible
            # Use XzEncoder (XZ Utils LZMA)
            XzEncoderAdapter.new(output, options)
          else
            # Use SdkEncoder (7-Zip LZMA SDK compatible) - DEFAULT
            Implementations::SevenZip::LZMA::Encoder.new(output,
                                                         options)
          end
end

Instance Attribute Details

#dict_sizeObject (readonly)

Returns the value of attribute dict_size.



42
43
44
# File 'lib/omnizip/algorithms/lzma/encoder.rb', line 42

def dict_size
  @dict_size
end

#lcObject (readonly)

Returns the value of attribute lc.



42
43
44
# File 'lib/omnizip/algorithms/lzma/encoder.rb', line 42

def lc
  @lc
end

#lpObject (readonly)

Returns the value of attribute lp.



42
43
44
# File 'lib/omnizip/algorithms/lzma/encoder.rb', line 42

def lp
  @lp
end

#pbObject (readonly)

Returns the value of attribute pb.



42
43
44
# File 'lib/omnizip/algorithms/lzma/encoder.rb', line 42

def pb
  @pb
end

Instance Method Details

#encode_stream(input) ⇒ Array<String, Integer>, void

Encode a stream of data

Parameters:

  • input (String, IO)

    Input data to compress

Returns:

  • (Array<String, Integer>, void)

    Tuple of [data, decode_bytes] in raw mode, void otherwise



84
85
86
# File 'lib/omnizip/algorithms/lzma/encoder.rb', line 84

def encode_stream(input)
  @impl.encode_stream(input)
end