Class: Omnizip::Algorithms::XZLZMA2

Inherits:
Omnizip::Algorithm show all
Defined in:
lib/omnizip/algorithms/xz_lzma2.rb

Overview

XZ Utils LZMA2 compression algorithm.

This algorithm uses the XZ Utils implementation of LZMA2, which provides full compatibility with xz command-line tools.

Examples:

Compress data

algorithm = Omnizip::AlgorithmRegistry.get(:xz_lzma2).new(
  dict_size: 8 * 1024 * 1024
)
algorithm.compress(input_io, output_io)

Decompress data

algorithm = Omnizip::AlgorithmRegistry.get(:xz_lzma2).new
algorithm.decompress(input_io, output_io)

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

.metadataModels::AlgorithmMetadata

Get algorithm metadata.

Returns:



43
44
45
46
47
48
49
# File 'lib/omnizip/algorithms/xz_lzma2.rb', line 43

def self.
  Models::AlgorithmMetadata.new.tap do |meta|
    meta.name = "xz_lzma2"
    meta.description = "LZMA2 compression (XZ Utils implementation)"
    meta.version = "1.0.0"
  end
end

Instance Method Details

#compress(input, output) ⇒ void

This method returns an undefined value.

Compress data using XZ Utils LZMA2.

Parameters:

  • input (IO, String)

    Input data

  • output (IO)

    Output stream



56
57
58
59
60
61
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
# File 'lib/omnizip/algorithms/xz_lzma2.rb', line 56

def compress(input, output)
  input_data = if input.is_a?(String)
                 input
               elsif input.respond_to?(:read)
                 input.read
               else
                 raise ArgumentError, "Input must be a String or IO"
               end

  # Apply filter if set
  if @filter
    input_data = @filter.encode(input_data)
  end

  # Get encoding options
  dict_size = @options.fetch(:dict_size, 8 * 1024 * 1024)
  lc = @options.fetch(:lc, 3)
  lp = @options.fetch(:lp, 0)
  pb = @options.fetch(:pb, 2)
  standalone = @options.fetch(:standalone, false)

  # Create encoder and compress
  encoder = Implementations::XZUtils::LZMA2::Encoder.new(
    dict_size: dict_size,
    lc: lc,
    lp: lp,
    pb: pb,
    standalone: standalone,
  )

  compressed = encoder.encode(input_data)
  output.write(compressed)
end

#decompress(input, output) ⇒ void

This method returns an undefined value.

Decompress data using XZ Utils LZMA2.

Parameters:

  • input (IO)

    Input stream

  • output (IO)

    Output stream



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/omnizip/algorithms/xz_lzma2.rb', line 95

def decompress(input, output)
  dict_size = @options.fetch(:dict_size, 8 * 1024 * 1024)

  decoder = Implementations::XZUtils::LZMA2::Decoder.new(input,
                                                         raw_mode: true, dict_size: dict_size)
  decompressed = decoder.decode_stream

  # Reverse filter if set
  if @filter
    decompressed = @filter.decode(decompressed)
  end

  output.write(decompressed)
end