Class: Omnizip::Algorithms::LZMA2

Inherits:
Omnizip::Algorithm show all
Defined in:
lib/omnizip/algorithms/lzma2.rb,
lib/omnizip/algorithms/lzma2.rb,
lib/omnizip/algorithms/lzma2/properties.rb,
lib/omnizip/algorithms/lzma2/lzma2_chunk.rb,
lib/omnizip/algorithms/lzma2/chunk_manager.rb,
lib/omnizip/algorithms/lzma2/simple_lzma2_encoder.rb

Overview

LZMA2 compression algorithm Improved version of LZMA with chunked format for better streaming

Defined Under Namespace

Classes: ChunkManager, LZMA2Chunk, Properties, SimpleLZMA2Encoder

Instance Attribute Summary

Attributes inherited from Omnizip::Algorithm

#filter, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Omnizip::Algorithm

inherited, #with_filter

Constructor Details

#initialize(options = {}) ⇒ LZMA2

Returns a new instance of LZMA2.



60
61
62
63
64
65
66
67
68
# File 'lib/omnizip/algorithms/lzma2.rb', line 60

def initialize(options = {})
  super()
  @dict_size = options[:dict_size] || (8 * 1024 * 1024) # 8 MB default
  @lc = options[:lc] || 3
  @lp = options[:lp] || 0
  @pb = options[:pb] || 2
  @level = options[:level] || 6
  @raw_mode = options[:raw_mode] # For 7-Zip format (no property byte)
end

Class Method Details

.encode_dict_size(dict_size) ⇒ Object

Encode dictionary size as single byte for LZMA2 properties



131
132
133
# File 'lib/omnizip/algorithms/lzma2.rb', line 131

def self.encode_dict_size(dict_size)
  LZMA2Encoder.encode_dict_size(dict_size)
end

.metadataModels::AlgorithmMetadata

Get algorithm metadata

Returns:



50
51
52
53
54
55
56
57
# File 'lib/omnizip/algorithms/lzma2.rb', line 50

def 
  Models::AlgorithmMetadata.new.tap do |meta|
    meta.name = "lzma2"
    meta.description = "LZMA2 compression with improved chunking format for better streaming"
    meta.version = "1.0.0"
    meta.supports_streaming = true
  end
end

Instance Method Details

#compress(input, output, options = {}) ⇒ Object

Compress data using LZMA2



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/omnizip/algorithms/lzma2.rb', line 71

def compress(input, output, options = {})
  # For 7-Zip format, use raw_mode (no property byte in data stream)
  # Standalone LZMA2 files include a property byte
  standalone = options.fetch(:standalone, true)
  options.fetch(:raw_mode, !standalone)

  encoder = LZMA2Encoder.new(
    dict_size: @dict_size,
    lc: @lc,
    lp: @lp,
    pb: @pb,
    standalone: standalone, # Write property byte only for standalone files
  )

  # Read input
  input_data = input.respond_to?(:read) ? input.read : input

  # Encode with LZMA2
  compressed = encoder.encode(input_data)

  # Write to output
  if output.respond_to?(:write)
    output.write(compressed)
  else
    output.replace(compressed)
  end
end

#decompress(input, output, options = {}) ⇒ Object

Decompress LZMA2 data



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/omnizip/algorithms/lzma2.rb', line 100

def decompress(input, output, options = {})
  # Read input data
  input_data = input.respond_to?(:read) ? input.read : input
  input_stream = StringIO.new(input_data)
  input_stream.set_encoding(Encoding::BINARY)

  # Determine raw_mode:
  # - For 7-Zip format: raw_mode=true, dict_size from coder properties
  # - For standalone LZMA2 files: raw_mode=false, dict_size from property byte
  raw_mode = options.fetch(:raw_mode, @raw_mode || false)
  dict_size = options.fetch(:dict_size, @dict_size)

  # Create decoder using XZ Utils implementation
  decoder = Omnizip::Implementations::XZUtils::LZMA2::Decoder.new(
    input_stream,
    raw_mode: raw_mode,
    dict_size: dict_size,
  )

  # Decode LZMA2 data
  decompressed = decoder.decode_stream

  # Write to output
  if output.respond_to?(:write)
    output.write(decompressed)
  else
    output.replace(decompressed)
  end
end