Class: IOStreams::Zip::Reader

Inherits:
Reader
  • Object
show all
Defined in:
lib/io_streams/zip/reader.rb

Instance Attribute Summary

Attributes inherited from Reader

#input_stream

Class Method Summary collapse

Methods inherited from Reader

#initialize, open, stream

Constructor Details

This class inherits a constructor from IOStreams::Reader

Class Method Details

.file(file_name, entry_file_name: nil, &block) ⇒ Object

Read from a zip file or stream, decompressing the contents as it is read The input stream from the first file found in the zip file is passed to the supplied block



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/io_streams/zip/reader.rb', line 22

def self.file(file_name, entry_file_name: nil)
  fin = Java::JavaIo::FileInputStream.new(file_name)
  zin = Java::JavaUtilZip::ZipInputStream.new(fin)

  get_entry(zin, entry_file_name) ||
    raise(Java::JavaUtilZip::ZipException, "File #{entry_file_name} not found within zip file.")

  yield(zin.to_io)
ensure
  zin&.close
  fin&.close
end

.get_entry(zin, entry_file_name) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/io_streams/zip/reader.rb', line 35

def self.get_entry(zin, entry_file_name)
  if entry_file_name.nil?
    zin.get_next_entry
    return true
  end

  while entry = zin.get_next_entry
    return true if entry.name == entry_file_name
  end
  false
end