class ZipContainer::File
This class represents a ZipContainer
file in PK Zip format. See the OCF and UCF specifications for more details.
This class provides most of the facilities of the Zip::File
class in the rubyzip gem. Please also consult the rubyzip documentation alongside these pages.
There are code examples available with the source code of this library.
Public Class Methods
Source
# File lib/zip-container/file.rb 77 def self.create(filename, mimetype) 78 ::Zip::OutputStream.open(filename) do |stream| 79 stream.put_next_entry(MIMETYPE_FILE, nil, nil, ::Zip::Entry::STORED) 80 stream.write mimetype 81 end 82 83 # Now open the newly created container. 84 c = new(filename) 85 86 if block_given? 87 begin 88 yield c 89 ensure 90 c.close 91 end 92 end 93 94 c 95 end
Create a new ZipContainer
file on disk with the specified mimetype.
Source
# File lib/zip-container/file.rb 104 def self.each_entry(filename, &block) 105 c = new(filename) 106 107 if block_given? 108 begin 109 c.each(&block) 110 ensure 111 c.close 112 end 113 end 114 115 c.each 116 end
Iterate over the entries in the ZipContainer
file. The entry objects returned by this method are Zip::Entry objects. Please see the rubyzip documentation for details.
Public Instance Methods
Source
# File lib/zip-container/file.rb 128 def add(entry, src_path, &continue_on_exists_proc) 129 if reserved_entry?(entry) || managed_directory?(entry) 130 raise ReservedNameClashError, entry.to_s 131 end 132 133 @container.add(entry, src_path, &continue_on_exists_proc) 134 end
Convenience method for adding the contents of a file to the ZipContainer
file. If asked to add a file with a reserved name, such as the special mimetype header file, this method will raise a ReservedNameClashError
.
See the rubyzip documentation for details of the continue_on_exists_proc
parameter.
Source
# File lib/zip-container/file.rb 375
Returns the ZipContainer
file comment, if it has one.
Source
# File lib/zip-container/file.rb 382
Set the ZipContainer
file comment to the new value.
Source
# File lib/zip-container/file.rb 143 def commit 144 return false unless commit_required? 145 146 @container.commit if on_disk? 147 end
Commits changes that have been made since the previous commit to the ZipContainer
file. Returns true
if anything was actually done, false
otherwise.
Source
# File lib/zip-container/file.rb 389
Returns true
if any changes have been made to this ZipContainer
file since the last commit, false
otherwise.
Source
# File lib/zip-container/file.rb 159 def dir 160 @fs_dir 161 end
Returns an object which can be used like ruby’s built in Dir
(class) object, except that it works on the ZipContainer
file on which this method is invoked.
See the rubyzip documentation for details.
Source
# File lib/zip-container/file.rb 397
Iterate over the entries in the ZipContainer
file. The entry objects returned by this method are Zip::Entry objects. Please see the rubyzip documentation for details.
Source
# File lib/zip-container/file.rb 407
Returns an Enumerable containing all the entries in the ZipContainer
file The entry objects returned by this method are Zip::Entry objects. Please see the rubyzip documentation for details.
Source
# File lib/zip-container/file.rb 416
Extracts the specified entry of the ZipContainer
file to dest_path
.
See the rubyzip documentation for details of the on_exists_proc
parameter.
Source
# File lib/zip-container/file.rb 171 def file 172 @fs_file 173 end
Returns an object which can be used like ruby’s built in File
(class) object, except that it works on the ZipContainer
file on which this method is invoked.
See the rubyzip documentation for details.
Source
# File lib/zip-container/file.rb 182 def find_entry(entry_name, options = {}) 183 options = { include_hidden: false }.merge(options) 184 185 unless options[:include_hidden] 186 return if hidden_entry?(entry_name) 187 end 188 189 @container.find_entry(entry_name) 190 end
Searches for the entry with the specified name. Returns nil
if no entry is found or if the specified entry is hidden for normal use. You can specify :include_hidden => true
to include hidden entries in the search.
Source
# File lib/zip-container/file.rb 199 def get_entry(entry, options = {}) 200 options = { include_hidden: false }.merge(options) 201 202 unless options[:include_hidden] 203 raise Errno::ENOENT, entry if hidden_entry?(entry) 204 end 205 206 @container.get_entry(entry) 207 end
Searches for an entry like find_entry
, but throws Errno::ENOENT if no entry is found or if the specified entry is hidden for normal use. You can specify :include_hidden => true
to include hidden entries in the search.
Source
# File lib/zip-container/file.rb 442
Returns an input stream to the specified entry. If a block is passed the stream object is passed to the block and the stream is automatically closed afterwards just as with ruby’s built in File.open
method.
Source
# File lib/zip-container/file.rb 219 def get_output_stream(entry, permission = nil, &block) 220 if reserved_entry?(entry) || managed_directory?(entry) 221 raise ReservedNameClashError, entry.to_s 222 end 223 224 @container.get_output_stream(entry, permission, &block) 225 end
Returns an output stream to the specified entry. If a block is passed the stream object is passed to the block and the stream is automatically closed afterwards just as with ruby’s built-in File.open
method.
See the rubyzip documentation for details of the permission_int
parameter.
Source
# File lib/zip-container/file.rb 241 def glob(pattern, *params) 242 flags = ::File::FNM_PATHNAME | ::File::FNM_DOTMATCH 243 options = { include_hidden: false } 244 245 params.each do |param| 246 case param 247 when Hash 248 options = options.merge(param) 249 else 250 flags = param 251 end 252 end 253 254 entries.map do |entry| 255 next if !options[:include_hidden] && hidden_entry?(entry) 256 next unless ::File.fnmatch(pattern, entry.name.chomp('/'), flags) 257 258 yield(entry) if block_given? 259 entry 260 end.compact 261 end
Searches for entries given a glob. Hidden files are ignored by default.
The parameters that can be supplied are:
-
flags
- A bitwise OR of theFNM_xxx
parameters defined in File::Constants. The default value is::File::FNM_PATHNAME | ::File::FNM_DOTMATCH
-
options
-:include_hidden => true
will include hidden entries in the search.
Source
# File lib/zip-container/file.rb 267 def in_memory? 268 !@on_disk 269 end
Is this ZipContainer
file memory resident as opposed to stored on disk?
Source
# File lib/zip-container/file.rb 281 def mkdir(name, permission = 0o0755) 282 if reserved_entry?(name) || managed_file?(name) 283 raise ReservedNameClashError, name 284 end 285 286 @container.mkdir(name, permission) 287 end
Creates a directory in the ZipContainer
file. If asked to create a directory with a name reserved for use by a file this method will raise a ReservedNameClashError
.
The new directory will be created with the supplied unix-style permissions. The default (0755
) is owner read, write and list; group read and list; and world read and list.
Source
# File lib/zip-container/file.rb 464
Returns the filename of this ZipContainer
file.
Source
# File lib/zip-container/file.rb 293 def on_disk? 294 @on_disk 295 end
Is this ZipContainer
file stored on disk as opposed to memory resident?
Source
# File lib/zip-container/file.rb 471
Returns a string containing the contents of the specified entry.
Source
# File lib/zip-container/file.rb 303 def remove(entry) 304 return if reserved_entry?(entry) 305 306 @container.remove(entry) 307 end
Removes the specified entry from the ZipContainer
file. If asked to remove any reserved files such as the special mimetype header file this method will do nothing.
Source
# File lib/zip-container/file.rb 319 def rename(entry, new_name, &continue_on_exists_proc) 320 return if reserved_entry?(entry) 321 raise ReservedNameClashError, new_name if reserved_entry?(new_name) 322 323 @container.rename(entry, new_name, &continue_on_exists_proc) 324 end
Renames the specified entry in the ZipContainer
file. If asked to rename any reserved files such as the special mimetype header file this method will do nothing. If asked to rename a file to one of the reserved names a ReservedNameClashError
is raised.
See the rubyzip documentation for details of the continue_on_exists_proc
parameter.
Source
# File lib/zip-container/file.rb 333 def replace(entry, src_path) 334 return if reserved_entry?(entry) 335 336 @container.replace(entry, src_path) 337 end
Replaces the specified entry of the ZipContainer
file with the contents of src_path
(from the file system). If asked to replace any reserved files such as the special mimetype header file this method will do nothing.
Source
# File lib/zip-container/file.rb 478
Returns the number of entries in the ZipContainer
file.
Source
# File lib/zip-container/file.rb 343 def to_s 344 @container.to_s + " - #{@mimetype}" 345 end
Return a textual summary of this ZipContainer
file.