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 on the native
Formats::Zip tree. In-place entry editing (+add_entry+ /
remove_entry) still goes through the rubyzip-compat
Omnizip::Zip::File layer: it is an in-place central-directory
rewriter the native tree does not have (the same reason the
Metadata subsystem lives there).
Defined Under Namespace
Classes: WriterProxy
Instance Method Summary collapse
- #add_entry(path, entry_name, source_path) ⇒ Object
- #create(path, compression_method: nil, level: nil, **_, &block) ⇒ Object
- #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
71 72 73 |
# File 'lib/omnizip/archive_handlers/zip_handler.rb', line 71 def add_entry(path, entry_name, source_path) Omnizip::Zip::File.open(path) { |zip| zip.add(entry_name, source_path) } end |
#create(path, compression_method: nil, level: nil, **_, &block) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/omnizip/archive_handlers/zip_handler.rb', line 13 def create(path, compression_method: nil, level: nil, **_, &block) writer = Formats::Zip::Writer.new(path) block&.call(WriterProxy.new(writer)) # The native writer takes archive-level compression at #write # time; keep profile keywords out of it kwargs = {} kwargs[:compression_method] = compression_method if compression_method kwargs[:level] = level if level writer.write(**kwargs) path end |
#extract_to(path, output_dir, overwrite: false, **_) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/omnizip/archive_handlers/zip_handler.rb', line 31 def extract_to(path, output_dir, overwrite: false, **_) reader = Formats::Zip::Reader.new(path) if overwrite reader.extract_all(output_dir) reader.entries.map { |e| ::File.join(output_dir, e.filename) } else extracted = [] reader.entries.each do |entry| dest_path = ::File.join(output_dir, entry.filename) raise "File exists: #{dest_path}" if ::File.exist?(dest_path) reader.extract_entry(entry, output_dir) extracted << dest_path end extracted end end |
#list(path, details: false, **_) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/omnizip/archive_handlers/zip_handler.rb', line 50 def list(path, details: false, **_) reader = Formats::Zip::Reader.new(path) return reader.entries.map(&:filename) unless details reader.entries.map do |entry| { name: entry.filename, size: entry.uncompressed_size, compressed_size: entry.compressed_size, compression_method: entry.compression_method, crc: entry.crc32, time: entry.time, directory: entry.directory?, } end end |
#open(path, &block) ⇒ Object
26 27 28 29 |
# File 'lib/omnizip/archive_handlers/zip_handler.rb', line 26 def open(path, &block) reader = Formats::Zip::Reader.new(path) block ? yield(reader) : reader end |
#read_entry(path, entry_name) ⇒ Object
67 68 69 |
# File 'lib/omnizip/archive_handlers/zip_handler.rb', line 67 def read_entry(path, entry_name) Formats::Zip::Reader.new(path).read_entry(entry_name) end |