Class: Xlsxrb::Ooxml::ZipReader

Inherits:
Object
  • Object
show all
Defined in:
lib/xlsxrb/ooxml/zip_reader.rb

Overview

Reads ZIP archives using only stdlib (zlib). Scans local file headers sequentially — works with non-seekable IO.

Constant Summary collapse

LOCAL_HEADER_SIG =
[0x50, 0x4B, 0x03, 0x04].pack("C4")

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ ZipReader

Returns a new instance of ZipReader.



28
29
30
31
# File 'lib/xlsxrb/ooxml/zip_reader.rb', line 28

def initialize(io)
  @io = io
  @entries = nil
end

Class Method Details

.open(source) ⇒ Object

Opens a ZIP from a file path or IO and yields the reader.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/xlsxrb/ooxml/zip_reader.rb', line 14

def self.open(source)
  io = source.is_a?(String) ? File.open(source, "rb") : source
  reader = new(io)
  if block_given?
    begin
      yield reader
    ensure
      io.close if source.is_a?(String)
    end
  else
    reader
  end
end

Instance Method Details

#each_entry(&block) ⇒ Object

Yields (entry_name, data_string) for each file in the archive.



46
47
48
49
50
# File 'lib/xlsxrb/ooxml/zip_reader.rb', line 46

def each_entry(&block)
  return enum_for(:each_entry) unless block

  entries.each_pair(&block)
end

#read_allObject

Returns a Hash { entry_name => raw_bytes } for all entries.



34
35
36
37
38
# File 'lib/xlsxrb/ooxml/zip_reader.rb', line 34

def read_all
  result = {}
  each_entry { |name, data| result[name] = data }
  result
end

#read_entry(name) ⇒ Object

Returns raw bytes for a single entry, or nil if not found.



41
42
43
# File 'lib/xlsxrb/ooxml/zip_reader.rb', line 41

def read_entry(name)
  entries[name]
end