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-next 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-next Naming/BlockForwarding, Style/ArgumentsForwarding, Style/BlockDelimiters -- Ruby 3.0 compatibility.
-
#glob(pattern, &block) ⇒ Array<Entry>
Glob entries by pattern rubocop:disable-next 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-next 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
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/omnizip/zip/file.rb', line 45 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
172 173 174 |
# File 'lib/omnizip/zip/file.rb', line 172 def comment @comment end |
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
13 14 15 |
# File 'lib/omnizip/zip/file.rb', line 13 def entries @entries end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/omnizip/zip/file.rb', line 13 def name @name end |
Class Method Details
.create(file_path) {|file| ... } ⇒ Object
Create a new archive from scratch
38 39 40 |
# File 'lib/omnizip/zip/file.rb', line 38 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
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/omnizip/zip/file.rb', line 21 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
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/omnizip/zip/file.rb', line 69 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
290 291 292 293 294 |
# File 'lib/omnizip/zip/file.rb', line 290 def close commit if @modified @reader = nil @writer = nil end |
#commit ⇒ Object
Commit changes to disk
283 284 285 286 287 |
# File 'lib/omnizip/zip/file.rb', line 283 def commit write_archive if @modified @modified = false @reader = nil # Invalidate reader after writing end |
#count_matching(pattern) ⇒ Integer
Count files matching a pattern
256 257 258 |
# File 'lib/omnizip/zip/file.rb', line 256 def count_matching(pattern) Omnizip::Extraction.count_matching(self, pattern) end |
#each {|entry| ... } ⇒ Object
Iterate over all entries rubocop:disable-next Naming/BlockForwarding, Style/ArgumentsForwarding -- Ruby 3.0 compatibility
116 117 118 |
# File 'lib/omnizip/zip/file.rb', line 116 def each(&block) entries.each(&block) end |
#extract(entry, dest_path, &on_exists_proc) ⇒ Object
Extract an entry to a destination path
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/omnizip/zip/file.rb', line 123 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
235 236 237 |
# File 'lib/omnizip/zip/file.rb', line 235 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
242 243 244 |
# File 'lib/omnizip/zip/file.rb', line 242 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
265 266 267 |
# File 'lib/omnizip/zip/file.rb', line 265 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
89 90 91 |
# File 'lib/omnizip/zip/file.rb', line 89 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-next Naming/BlockForwarding, Style/ArgumentsForwarding, Style/BlockDelimiters -- Ruby 3.0 compatibility
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/omnizip/zip/file.rb', line 99 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 StringIO.open(content, "rb", &block) else content end end |
#glob(pattern, &block) ⇒ Array<Entry>
Glob entries by pattern rubocop:disable-next Naming/BlockForwarding, Style/ArgumentsForwarding, Style/BlockDelimiters -- Ruby 3.0 compatibility
217 218 219 220 221 222 223 224 225 |
# File 'lib/omnizip/zip/file.rb', line 217 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
203 204 205 |
# File 'lib/omnizip/zip/file.rb', line 203 def include?(entry_name) !get_entry(entry_name).nil? end |
#list_matching(pattern) ⇒ Array<Entry>
List files matching a pattern
249 250 251 |
# File 'lib/omnizip/zip/file.rb', line 249 def list_matching(pattern) Omnizip::Extraction.list_matching(self, pattern) end |
#metadata ⇒ Omnizip::Metadata::ArchiveMetadata
Get archive metadata
271 272 273 |
# File 'lib/omnizip/zip/file.rb', line 271 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.
190 191 192 193 |
# File 'lib/omnizip/zip/file.rb', line 190 def modified! @modified = true self end |
#modified? ⇒ Boolean
True if there are unsaved changes.
196 197 198 |
# File 'lib/omnizip/zip/file.rb', line 196 def modified? @modified end |
#names ⇒ Array<String>
Get names of all entries
209 210 211 |
# File 'lib/omnizip/zip/file.rb', line 209 def names entries.map(&:name) end |
#remove(entry_name) ⇒ Object
Remove an entry from the archive
142 143 144 145 146 147 148 |
# File 'lib/omnizip/zip/file.rb', line 142 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
153 154 155 156 157 158 159 160 |
# File 'lib/omnizip/zip/file.rb', line 153 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-next Naming/BlockForwarding, Style/ArgumentsForwarding -- Ruby 3.0 compatibility
166 167 168 169 |
# File 'lib/omnizip/zip/file.rb', line 166 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
277 278 279 280 |
# File 'lib/omnizip/zip/file.rb', line 277 def @modified = true .reset_modified end |
#size ⇒ Object Also known as: length
Get number of entries
183 184 185 |
# File 'lib/omnizip/zip/file.rb', line 183 def size entries.size end |