Class: Omnizip::Algorithms::LZMA2::SimpleLZMA2Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/algorithms/lzma2/simple_lzma2_encoder.rb

Overview

Simple LZMA2 encoder using XzEncoder internally

This encoder uses the working XzEncoder for LZMA compression and wraps the result in proper LZMA2 chunks.

For 7-Zip format compatibility, we need to produce LZMA2 chunks without a leading property byte (raw mode).

Constant Summary collapse

UNCOMPRESSED_MAX =

Maximum uncompressed size per LZMA2 chunk (2MB)

1 << 21

Instance Method Summary collapse

Constructor Details

#initialize(dict_size: 8 * 1024 * 1024, lc: 3, lp: 0, pb: 2, standalone: true) ⇒ SimpleLZMA2Encoder

Initialize the encoder

Parameters:

  • dict_size (Integer) (defaults to: 8 * 1024 * 1024)

    Dictionary size (default: 8MB)

  • lc (Integer) (defaults to: 3)

    Literal context bits (default: 3)

  • lp (Integer) (defaults to: 0)

    Literal position bits (default: 0)

  • pb (Integer) (defaults to: 2)

    Position bits (default: 2)

  • standalone (Boolean) (defaults to: true)

    If true, write property byte at start



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/omnizip/algorithms/lzma2/simple_lzma2_encoder.rb', line 45

def initialize(
  dict_size: 8 * 1024 * 1024,
  lc: 3,
  lp: 0,
  pb: 2,
  standalone: true
)
  @dict_size = dict_size
  @lc = lc
  @lp = lp
  @pb = pb
  @standalone = standalone
end

Instance Method Details

#encode(input_data) ⇒ String

Encode data into LZMA2 format

Parameters:

  • input_data (String)

    Input data to compress

Returns:

  • (String)

    LZMA2 compressed data



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/omnizip/algorithms/lzma2/simple_lzma2_encoder.rb', line 62

def encode(input_data)
  output = StringIO.new
  output.set_encoding(Encoding::BINARY)

  # Write property byte if standalone
  # LZMA2 property byte encodes dictionary size
  if @standalone
    prop_byte = encode_dict_size(@dict_size)
    output.putc(prop_byte)
  end

  # Use XZ Utils LZMA2 encoder for proper LZMA2 encoding (no EOS marker)
  # Pass standalone: false since SimpleLZMA2Encoder handles property byte
  encoder = Omnizip::Implementations::XZUtils::LZMA2::Encoder.new(
    dict_size: @dict_size,
    lc: @lc,
    lp: @lp,
    pb: @pb,
    standalone: false,
  )

  # Encode data - returns LZMA2 data as String (includes end marker)
  encoded = encoder.encode(input_data)

  # Write encoded data to output
  output.write(encoded)

  output.string
end