Class: Xlsxrb::Ooxml::ZipReader
- Inherits:
-
Object
- Object
- Xlsxrb::Ooxml::ZipReader
- 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 =
"PK\x03\x04".b
- MAX_UNCOMPRESSED_SIZE =
500MB per file limit
500 * 1024 * 1024
Class Method Summary collapse
-
.open(source) ⇒ Object
Opens a ZIP from a file path or IO and yields the reader.
Instance Method Summary collapse
-
#each_entry(&block) ⇒ Object
Yields (entry_name, data_string) for each file in the archive.
-
#initialize(io) ⇒ ZipReader
constructor
A new instance of ZipReader.
-
#read_all ⇒ Object
Returns a Hash { entry_name => raw_bytes } for all entries.
-
#read_entry(name) ⇒ Object
Returns raw bytes for a single entry, or nil if not found.
Constructor Details
#initialize(io) ⇒ ZipReader
Returns a new instance of ZipReader.
32 33 34 35 |
# File 'lib/xlsxrb/ooxml/zip_reader.rb', line 32 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.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/xlsxrb/ooxml/zip_reader.rb', line 18 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.
50 51 52 53 54 |
# File 'lib/xlsxrb/ooxml/zip_reader.rb', line 50 def each_entry(&block) return enum_for(:each_entry) unless block entries.each_pair(&block) end |
#read_all ⇒ Object
Returns a Hash { entry_name => raw_bytes } for all entries.
38 39 40 41 42 |
# File 'lib/xlsxrb/ooxml/zip_reader.rb', line 38 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.
45 46 47 |
# File 'lib/xlsxrb/ooxml/zip_reader.rb', line 45 def read_entry(name) entries[name] end |