Class: Omnizip::Formats::Xz

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/formats/xz.rb,
lib/omnizip/formats/xz/reader.rb,
lib/omnizip/formats/xz/writer.rb

Overview

XZ compression format Creates .xz files compatible with XZ Utils

Defined Under Namespace

Classes: Builder, Entry, Reader, Writer

Class Method Summary collapse

Class Method Details

.create(input, output = nil, options = {}) ⇒ Object

Create a .xz file from input data

Parameters:

  • input (String, IO)

    Input data to compress

  • output (String, IO) (defaults to: nil)

    Output file path or IO object

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

    Compression options

Options Hash (options):

  • :dict_size (Integer)

    Dictionary size (default: 8MB to match XZ Utils preset 6)

  • :check (Integer)

    Check type (default: CRC64)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/omnizip/formats/xz.rb', line 19

def create(input, output = nil, options = {})
  encoder = ::Omnizip::Formats::XzImpl::StreamEncoder.new(
    check_type: options[:check] || ::Omnizip::Formats::XzConst::CHECK_CRC64,
    dict_size: options[:dict_size] || (64 * 1024 * 1024),
  )

  compressed = encoder.encode(input)

  if output
    Omnizip::IO::Sink.for(output).write(compressed)
  else
    compressed
  end
end

.create_file(path, options = {}) {|builder| ... } ⇒ Object

Convenience method with block syntax

Examples:

Xz.create_file('output.xz') do |xz|
  xz.add_data('Hello, XZ!')
end

Yields:

  • (builder)


39
40
41
42
43
44
45
# File 'lib/omnizip/formats/xz.rb', line 39

def create_file(path, options = {})
  builder = Builder.new(options)
  yield builder if block_given?

  compressed = create(builder.data, nil, options)
  File.binwrite(path, compressed)
end

.decompress(input, output = nil, _options = {}) ⇒ String? Also known as: decode, extract

Decompress XZ, LZIP (.lz), or LZMA_Alone (.lzma) data

This method automatically detects the format based on magic bytes and routes to the appropriate decoder:

  • XZ format (.xz): Container format with stream header/footer/index
  • LZIP format (.lz): Standalone format with "LZIP" magic and CRC32 footer
  • LZMA_Alone format (.lzma): Legacy standalone format with properties byte

Parameters:

  • input (String, IO)

    Input data or file path

  • output (String, IO, nil) (defaults to: nil)

    Output file path or IO object

  • options (Hash)

    Options (reserved for future use)

Returns:

  • (String, nil)

    Decompressed data (if output is nil)



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/omnizip/formats/xz.rb', line 66

def decompress(input, output = nil, _options = {})
  data = Omnizip::IO::Source.for(input).read

  # Detect format and decode
  decompressed = decode_lzma_data(data)

  if output
    Omnizip::IO::Sink.for(output).write(decompressed)
    nil
  else
    decompressed
  end
end