Class: Omnizip::Formats::Xz::Reader

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

Overview

XZ format reader

Reads and decompresses .xz files compatible with XZ Utils. Provides both low-level stream API and high-level convenience methods.

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Reader

Initialize reader

Parameters:

  • input (String, IO)

    File path or IO object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/omnizip/formats/xz/reader.rb', line 36

def initialize(input)
  @input = if input.is_a?(String)
             File.open(input, "rb")
           elsif input.is_a?(::IO) || input.is_a?(StringIO)
             input
           else
             raise ArgumentError,
                   "Input must be a file path or IO object"
           end
  @close_on_finish = input.is_a?(String)
end

Instance Method Details

#closeObject

Close the input stream if we opened it



58
59
60
61
62
# File 'lib/omnizip/formats/xz/reader.rb', line 58

def close
  @input.close unless @input.closed?
rescue IOError
  nil
end

#closed?Boolean

Check if reader is open

Returns:

  • (Boolean)

    True if input is open



67
68
69
70
71
# File 'lib/omnizip/formats/xz/reader.rb', line 67

def closed?
  @input.closed?
rescue IOError
  true
end

#each_chunk(chunk_size = 64 * 1024) {|String| ... } ⇒ String

Read in a streaming fashion (for large files)

Yields:

  • (String)

    Chunks of decompressed data

Returns:

  • (String)

    Full decompressed data



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/omnizip/formats/xz/reader.rb', line 77

def each_chunk(chunk_size = 64 * 1024)
  # For now, just read everything and yield chunks
  # TODO: Implement true streaming for memory efficiency
  data = read
  offset = 0
  while offset < data.bytesize
    chunk = data.byteslice(offset, chunk_size)
    yield chunk
    offset += chunk.bytesize
  end
  data
end

#readString

Read and decompress XZ data

Returns:

  • (String)

    Decompressed data



51
52
53
54
55
# File 'lib/omnizip/formats/xz/reader.rb', line 51

def read
  Omnizip::Formats::XzImpl::StreamDecoder.decode(@input)
ensure
  close if @close_on_finish
end