Class: Omnizip::Algorithms::LZMA2
- Inherits:
-
Omnizip::Algorithm
- Object
- Omnizip::Algorithm
- Omnizip::Algorithms::LZMA2
- 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
Class Method Summary collapse
-
.encode_dict_size(dict_size) ⇒ Object
Encode dictionary size as single byte for LZMA2 properties.
-
.metadata ⇒ Models::AlgorithmMetadata
Get algorithm metadata.
Instance Method Summary collapse
-
#compress(input, output, options = {}) ⇒ Object
Compress data using LZMA2.
-
#decompress(input, output, options = {}) ⇒ Object
Decompress LZMA2 data.
-
#initialize(options = {}) ⇒ LZMA2
constructor
A new instance of LZMA2.
Methods inherited from Omnizip::Algorithm
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( = {}) super() @dict_size = [:dict_size] || (8 * 1024 * 1024) # 8 MB default @lc = [:lc] || 3 @lp = [:lp] || 0 @pb = [:pb] || 2 @level = [:level] || 6 @raw_mode = [: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 |
.metadata ⇒ Models::AlgorithmMetadata
Get algorithm metadata
50 51 52 53 54 55 56 57 |
# File 'lib/omnizip/algorithms/lzma2.rb', line 50 def Models::AlgorithmMetadata.new.tap do || .name = "lzma2" .description = "LZMA2 compression with improved chunking format for better streaming" .version = "1.0.0" .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, = {}) # For 7-Zip format, use raw_mode (no property byte in data stream) # Standalone LZMA2 files include a property byte standalone = .fetch(:standalone, true) .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, = {}) # 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 = .fetch(:raw_mode, @raw_mode || false) dict_size = .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 |