Class: Omnizip::Zip::File
- Inherits:
-
Object
- Object
- Omnizip::Zip::File
- Includes:
- Enumerable
- Defined in:
- lib/omnizip/zip/file.rb
Overview
Rubyzip-compatible File class Provides drop-in replacement for Zip::File from rubyzip
Instance Attribute Summary collapse
-
#comment ⇒ Object
Get archive comment.
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.create(file_path) {|file| ... } ⇒ Object
Create a new archive from scratch.
-
.open(file_path, create: false, **options) {|file| ... } ⇒ File
Open a ZIP archive.
Instance Method Summary collapse
-
#add(entry_name, src_path = nil) { ... } ⇒ Object
Add a file to the archive.
-
#close ⇒ Object
Close the archive.
-
#commit ⇒ Object
Commit changes to disk.
-
#count_matching(pattern) ⇒ Integer
Count files matching a pattern.
-
#each {|entry| ... } ⇒ Object
Iterate over all entries rubocop:disable Naming/BlockForwarding, Style/ArgumentsForwarding -- Ruby 3.0 compatibility.
-
#extract(entry, dest_path, &on_exists_proc) ⇒ Object
Extract an entry to a destination path.
-
#extract_matching(pattern, dest, options = {}) ⇒ Array<String>
Extract files matching a pattern.
-
#extract_matching_to_memory(pattern) ⇒ Hash<String, String>
Extract files matching a pattern to memory.
-
#extract_with_filter(filter, dest, options = {}) ⇒ Array<String>
Extract with a filter chain.
-
#get_entry(entry_name) ⇒ Entry?
(also: #find_entry)
Get entry by name.
-
#get_input_stream(entry) {|stream| ... } ⇒ String
(also: #read)
Get input stream for an entry rubocop:disable Naming/BlockForwarding, Style/ArgumentsForwarding, Style/BlockDelimiters -- Ruby 3.0 compatibility.
-
#glob(pattern, &block) ⇒ Array<Entry>
Glob entries by pattern rubocop:disable Naming/BlockForwarding, Style/ArgumentsForwarding, Style/BlockDelimiters -- Ruby 3.0 compatibility.
-
#include?(entry_name) ⇒ Boolean
Check if archive contains an entry.
-
#initialize(file_path, create: false, **options) ⇒ File
constructor
Initialize a ZIP file.
-
#list_matching(pattern) ⇒ Array<Entry>
List files matching a pattern.
-
#metadata ⇒ Omnizip::Metadata::ArchiveMetadata
Get archive metadata.
-
#modified! ⇒ Object
Mark this archive as having unsaved changes.
-
#modified? ⇒ Boolean
True if there are unsaved changes.
-
#names ⇒ Array<String>
Get names of all entries.
-
#remove(entry_name) ⇒ Object
Remove an entry from the archive.
-
#rename(entry_name, new_name) ⇒ Object
Rename an entry.
-
#replace(entry_name, src_path = nil, &block) ⇒ Object
Replace entry content rubocop:disable Naming/BlockForwarding, Style/ArgumentsForwarding -- Ruby 3.0 compatibility.
-
#save_metadata ⇒ Object
Save metadata changes Marks the archive as modified so changes are written on close.
-
#size ⇒ Object
(also: #length)
Get number of entries.
Constructor Details
#initialize(file_path, create: false, **options) ⇒ File
Initialize a ZIP file
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/omnizip/zip/file.rb', line 44 def initialize(file_path, create: false, **) @name = file_path @entries = [] @comment = "" @create = create @options = @modified = false @reader = nil @writer = nil # Load existing archive if it exists if ::File.exist?(file_path) && !create load_archive elsif create # Will create on write/close @modified = true elsif !::File.exist?(file_path) raise Errno::ENOENT, "No such file or directory - #{file_path}" end end |
Instance Attribute Details
#comment ⇒ Object
Get archive comment
175 176 177 |
# File 'lib/omnizip/zip/file.rb', line 175 def comment @comment end |
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
12 13 14 |
# File 'lib/omnizip/zip/file.rb', line 12 def entries @entries end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/omnizip/zip/file.rb', line 12 def name @name end |
Class Method Details
.create(file_path) {|file| ... } ⇒ Object
Create a new archive from scratch
37 38 39 |
# File 'lib/omnizip/zip/file.rb', line 37 def self.create(file_path, &block) open(file_path, create: true, &block) end |
.open(file_path, create: false, **options) {|file| ... } ⇒ File
Open a ZIP archive
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/omnizip/zip/file.rb', line 20 def self.open(file_path, create: false, **) file = new(file_path, create: create, **) if block_given? begin yield(file) ensure file.close end else file end end |
Instance Method Details
#add(entry_name, src_path = nil) { ... } ⇒ Object
Add a file to the archive
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/omnizip/zip/file.rb', line 68 def add(entry_name, src_path = nil) # Handle directory entries (ending with /) if entry_name.end_with?("/") && !src_path && !block_given? add_directory(entry_name) elsif block_given? data = yield add_data(entry_name, data) elsif src_path add_file_from_path(entry_name, src_path) else raise ArgumentError, "Either src_path or block must be provided" end @modified = true self end |
#close ⇒ Object
Close the archive
294 295 296 297 298 |
# File 'lib/omnizip/zip/file.rb', line 294 def close commit if @modified @reader = nil @writer = nil end |
#commit ⇒ Object
Commit changes to disk
287 288 289 290 291 |
# File 'lib/omnizip/zip/file.rb', line 287 def commit write_archive if @modified @modified = false @reader = nil # Invalidate reader after writing end |
#count_matching(pattern) ⇒ Integer
Count files matching a pattern
260 261 262 |
# File 'lib/omnizip/zip/file.rb', line 260 def count_matching(pattern) Omnizip::Extraction.count_matching(self, pattern) end |
#each {|entry| ... } ⇒ Object
Iterate over all entries rubocop:disable Naming/BlockForwarding, Style/ArgumentsForwarding -- Ruby 3.0 compatibility
117 118 119 |
# File 'lib/omnizip/zip/file.rb', line 117 def each(&block) entries.each(&block) end |
#extract(entry, dest_path, &on_exists_proc) ⇒ Object
Extract an entry to a destination path
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/omnizip/zip/file.rb', line 125 def extract(entry, dest_path, &on_exists_proc) entry = get_entry(entry) if entry.is_a?(String) raise Errno::ENOENT, "Entry not found: #{entry}" unless entry # Handle existing file if ::File.exist?(dest_path) if on_exists_proc action = yield(entry, dest_path) return if action == false else raise "Destination file already exists: #{dest_path}" end end extract_entry_to_path(entry, dest_path) end |
#extract_matching(pattern, dest, options = {}) ⇒ Array<String>
Extract files matching a pattern
239 240 241 |
# File 'lib/omnizip/zip/file.rb', line 239 def extract_matching(pattern, dest, = {}) Omnizip::Extraction.extract_matching(self, pattern, dest, ) end |
#extract_matching_to_memory(pattern) ⇒ Hash<String, String>
Extract files matching a pattern to memory
246 247 248 |
# File 'lib/omnizip/zip/file.rb', line 246 def extract_matching_to_memory(pattern) Omnizip::Extraction.extract_to_memory_matching(self, pattern) end |
#extract_with_filter(filter, dest, options = {}) ⇒ Array<String>
Extract with a filter chain
269 270 271 |
# File 'lib/omnizip/zip/file.rb', line 269 def extract_with_filter(filter, dest, = {}) Omnizip::Extraction.extract_with_filter(self, filter, dest, ) end |
#get_entry(entry_name) ⇒ Entry? Also known as: find_entry
Get entry by name
88 89 90 |
# File 'lib/omnizip/zip/file.rb', line 88 def get_entry(entry_name) entries.find { |e| e.name == entry_name } end |
#get_input_stream(entry) {|stream| ... } ⇒ String Also known as: read
Get input stream for an entry rubocop:disable Naming/BlockForwarding, Style/ArgumentsForwarding, Style/BlockDelimiters -- Ruby 3.0 compatibility
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/omnizip/zip/file.rb', line 98 def get_input_stream(entry, &block) entry = get_entry(entry) if entry.is_a?(String) raise Errno::ENOENT, "Entry not found: #{entry}" unless entry content = read_entry_data(entry) if block require "stringio" StringIO.open(content, "rb", &block) else content end end |
#glob(pattern, &block) ⇒ Array<Entry>
Glob entries by pattern rubocop:disable Naming/BlockForwarding, Style/ArgumentsForwarding, Style/BlockDelimiters -- Ruby 3.0 compatibility
220 221 222 223 224 225 226 227 228 |
# File 'lib/omnizip/zip/file.rb', line 220 def glob(pattern, &block) matching = entries.select { |e| ::File.fnmatch(pattern, e.name) } if block matching.each(&block) else matching end end |
#include?(entry_name) ⇒ Boolean
Check if archive contains an entry
206 207 208 |
# File 'lib/omnizip/zip/file.rb', line 206 def include?(entry_name) !get_entry(entry_name).nil? end |
#list_matching(pattern) ⇒ Array<Entry>
List files matching a pattern
253 254 255 |
# File 'lib/omnizip/zip/file.rb', line 253 def list_matching(pattern) Omnizip::Extraction.list_matching(self, pattern) end |
#metadata ⇒ Omnizip::Metadata::ArchiveMetadata
Get archive metadata
275 276 277 |
# File 'lib/omnizip/zip/file.rb', line 275 def @metadata ||= Omnizip::Metadata::ArchiveMetadata.new(self) end |
#modified! ⇒ Object
Mark this archive as having unsaved changes. The next #commit
or #close will write the archive to disk.
193 194 195 196 |
# File 'lib/omnizip/zip/file.rb', line 193 def modified! @modified = true self end |
#modified? ⇒ Boolean
True if there are unsaved changes.
199 200 201 |
# File 'lib/omnizip/zip/file.rb', line 199 def modified? @modified end |
#names ⇒ Array<String>
Get names of all entries
212 213 214 |
# File 'lib/omnizip/zip/file.rb', line 212 def names entries.map(&:name) end |
#remove(entry_name) ⇒ Object
Remove an entry from the archive
144 145 146 147 148 149 150 |
# File 'lib/omnizip/zip/file.rb', line 144 def remove(entry_name) entry = get_entry(entry_name) @entries.delete(entry) if entry @modified = true @reader = nil self end |
#rename(entry_name, new_name) ⇒ Object
Rename an entry
155 156 157 158 159 160 161 162 |
# File 'lib/omnizip/zip/file.rb', line 155 def rename(entry_name, new_name) entry = get_entry(entry_name) return unless entry entry.header.filename = new_name @modified = true self end |
#replace(entry_name, src_path = nil, &block) ⇒ Object
Replace entry content rubocop:disable Naming/BlockForwarding, Style/ArgumentsForwarding -- Ruby 3.0 compatibility
168 169 170 171 |
# File 'lib/omnizip/zip/file.rb', line 168 def replace(entry_name, src_path = nil, &block) remove(entry_name) add(entry_name, src_path, &block) end |
#save_metadata ⇒ Object
Save metadata changes Marks the archive as modified so changes are written on close
281 282 283 284 |
# File 'lib/omnizip/zip/file.rb', line 281 def @modified = true .reset_modified end |
#size ⇒ Object Also known as: length
Get number of entries
186 187 188 |
# File 'lib/omnizip/zip/file.rb', line 186 def size entries.size end |