Class: Omnizip::Archive::ReaderSession
- Inherits:
-
Object
- Object
- Omnizip::Archive::ReaderSession
- Defined in:
- lib/omnizip/archive/reader_session.rb
Overview
Block argument (and return value) of Archive.open. Delegates
per operation to the resolved ArchiveHandler, which opens and
closes the underlying reader on every call — the session itself
holds no resources.
Instance Method Summary collapse
- #each_entry(&block) ⇒ Object
-
#entries ⇒ Object
Archive metadata records (see Archive::Entry).
-
#extract(entry_name, dest_path) ⇒ Object
Extract one entry to an explicit destination path.
- #extract_all(output_dir) ⇒ Object
-
#extract_matching(pattern, output_dir) ⇒ Object
Extract every entry whose name matches
patternunderoutput_dir, preserving archive-relative paths. -
#initialize(handler, path, options = {}) ⇒ ReaderSession
constructor
A new instance of ReaderSession.
-
#read(entry_name) ⇒ Object
Contents of a single entry, without extraction.
Constructor Details
#initialize(handler, path, options = {}) ⇒ ReaderSession
Returns a new instance of ReaderSession.
12 13 14 15 16 |
# File 'lib/omnizip/archive/reader_session.rb', line 12 def initialize(handler, path, = {}) @handler = handler @path = path @options = end |
Instance Method Details
#each_entry(&block) ⇒ Object
26 27 28 |
# File 'lib/omnizip/archive/reader_session.rb', line 26 def each_entry(&block) entries.each(&block) end |
#entries ⇒ Object
Archive metadata records (see Archive::Entry).
19 20 21 22 23 24 |
# File 'lib/omnizip/archive/reader_session.rb', line 19 def entries @handler.list(@path, details: true, **@options).map do |h| Entry.new(name: h[:name], size: h[:size], directory: h[:directory], mtime: h[:mtime] || h[:time]) end end |
#extract(entry_name, dest_path) ⇒ Object
Extract one entry to an explicit destination path.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/omnizip/archive/reader_session.rb', line 36 def extract(entry_name, dest_path) entry = entries.find { |e| e.name == entry_name } unless entry raise Errno::ENOENT, "Entry not found: #{entry_name}" end if entry.directory? ::FileUtils.mkdir_p(dest_path) return dest_path end ::FileUtils.mkdir_p(::File.dirname(dest_path)) ::File.binwrite(dest_path, read(entry_name)) dest_path end |
#extract_all(output_dir) ⇒ Object
52 53 54 |
# File 'lib/omnizip/archive/reader_session.rb', line 52 def extract_all(output_dir) @handler.extract_to(@path, output_dir, **@options) end |
#extract_matching(pattern, output_dir) ⇒ Object
Extract every entry whose name matches pattern under
output_dir, preserving archive-relative paths.
58 59 60 61 62 63 64 |
# File 'lib/omnizip/archive/reader_session.rb', line 58 def extract_matching(pattern, output_dir) ::FileUtils.mkdir_p(output_dir) entries.select do |e| !e.directory? && ::File.fnmatch?(pattern, e.name, ::File::FNM_PATHNAME) end.map { |e| extract(e.name, ::File.join(output_dir, e.name)) } end |
#read(entry_name) ⇒ Object
Contents of a single entry, without extraction.
31 32 33 |
# File 'lib/omnizip/archive/reader_session.rb', line 31 def read(entry_name) @handler.read_entry(@path, entry_name, **@options) end |