Class: Omnizip::ArchiveHandlers::ZipHandler
- Inherits:
-
Object
- Object
- Omnizip::ArchiveHandlers::ZipHandler
- Defined in:
- lib/omnizip/archive_handlers/zip_handler.rb
Overview
Adapter that exposes the canonical +create+/+open+/+extract_to+
/+list+ interface for the ZIP format. Wraps Omnizip::Zip::File.
Defined Under Namespace
Classes: FileProxy
Instance Method Summary collapse
- #add_entry(path, entry_name, source_path) ⇒ Object
-
#create(path, **_options, &block) ⇒ Object
Zip::File has no archive-level compression options (method and level are per-entry in Zip::OutputStream), so extra keywords — e.g.
- #extract_to(path, output_dir, overwrite: false, **_) ⇒ Object
- #list(path, details: false, **_) ⇒ Object
- #open(path, &block) ⇒ Object
- #read_entry(path, entry_name) ⇒ Object
- #remove_entry(path, entry_name) ⇒ Object
Instance Method Details
#add_entry(path, entry_name, source_path) ⇒ Object
67 68 69 |
# File 'lib/omnizip/archive_handlers/zip_handler.rb', line 67 def add_entry(path, entry_name, source_path) Omnizip::Zip::File.open(path) { |zip| zip.add(entry_name, source_path) } end |
#create(path, **_options, &block) ⇒ Object
Zip::File has no archive-level compression options (method and level are per-entry in Zip::OutputStream), so extra keywords — e.g. profile-injected ones — are absorbed here.
11 12 13 14 15 |
# File 'lib/omnizip/archive_handlers/zip_handler.rb', line 11 def create(path, **, &block) Omnizip::Zip::File.create(path) do |file| block&.call(FileProxy.new(file)) end end |
#extract_to(path, output_dir, overwrite: false, **_) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/omnizip/archive_handlers/zip_handler.rb', line 21 def extract_to(path, output_dir, overwrite: false, **_) extracted = [] Omnizip::Zip::File.open(path) do |zip| zip.each do |entry| dest_path = ::File.join(output_dir, entry.name) on_exists = if overwrite proc { true } else proc { |_e, p| raise "File exists: #{p}" } end zip.extract(entry, dest_path, &on_exists) extracted << dest_path end end extracted end |
#list(path, details: false, **_) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/omnizip/archive_handlers/zip_handler.rb', line 38 def list(path, details: false, **_) Omnizip::Zip::File.open(path) do |zip| if details zip.entries.map do |entry| { name: entry.name, size: entry.size, compressed_size: entry.compressed_size, compression_method: entry.compression_method, crc: entry.crc, time: entry.time, directory: entry.directory?, } end else zip.names end end end |
#open(path, &block) ⇒ Object
17 18 19 |
# File 'lib/omnizip/archive_handlers/zip_handler.rb', line 17 def open(path, &block) Omnizip::Zip::File.open(path, &block) end |
#read_entry(path, entry_name) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/omnizip/archive_handlers/zip_handler.rb', line 58 def read_entry(path, entry_name) Omnizip::Zip::File.open(path) do |zip| entry = zip.get_entry(entry_name) raise Errno::ENOENT, "Entry not found: #{entry_name}" unless entry zip.read(entry) end end |