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::ZipFile
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.
The mime-type of this ZipContainer file.
Create a new ZipContainer file on disk with the specified mimetype.
# File lib/zip-container/container.rb, line 95 def Container.create(filename, mimetype, &block) ::Zip::ZipOutputStream.open(filename) do |stream| stream.put_next_entry(MIMETYPE_FILE, nil, nil, ::Zip::ZipEntry::STORED) stream.write mimetype end # Now open the newly created container. c = new(filename) if block_given? begin yield c ensure c.close end end c end
Iterate over the entries in the ZipContainer file. The entry objects returned by this method are Zip::ZipEntry objects. Please see the rubyzip documentation for details.
# File lib/zip-container/container.rb, line 122 def Container.each_entry(filename, &block) c = new(filename) if block_given? begin c.each(&block) ensure c.close end end c.each end
Open an existing ZipContainer file from disk. It will be checked for conformance upon first access.
# File lib/zip-container/container.rb, line 142 def Container.open(filename, &block) c = new(filename) if block_given? begin yield c ensure c.close end end c end
Verify that the specified ZipContainer
file conforms to the specification. This method returns false
if there are any problems at all with the file (including if it cannot be
found).
# File lib/zip-container/container.rb, line 162 def Container.verify(filename) begin new(filename).verify! rescue return false end true end
Verify that the specified ZipContainer file conforms to the specification. This method raises exceptions when errors are found or if there is something fundamental wrong with the file itself (e.g. it cannot be found).
# File lib/zip-container/container.rb, line 179 def Container.verify!(filename) new(filename).verify! 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.
# File lib/zip-container/container.rb, line 193 def add(entry, src_path, &continue_on_exists_proc) if reserved_entry?(entry) || managed_directory?(entry) raise ReservedNameClashError.new(entry.to_s) end @zipfile.add(entry, src_path, &continue_on_exists_proc) end
Returns the ZipContainer file comment, if it has one.
# File lib/zip-container/container.rb, line 387
Set the ZipContainer file comment to the new value.
# File lib/zip-container/container.rb, line 394
Commits changes that have been made since the previous commit to the ZipContainer file. Returns
true
if anything was actually done, false
otherwise.
# File lib/zip-container/container.rb, line 208 def commit return false unless commit_required? if on_disk? @zipfile.commit end end
Returns true
if any changes have been made to this ZipContainer file since the last commit,
false
otherwise.
# File lib/zip-container/container.rb, line 402
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.
# File lib/zip-container/container.rb, line 226 def dir @fs_dir end
Iterate over the entries in the ZipContainer file. The entry objects returned by this method are Zip::ZipEntry objects. Please see the rubyzip documentation for details.
# File lib/zip-container/container.rb, line 412
Returns an Enumerable containing all the entries in the ZipContainer file The entry objects returned by this method are Zip::ZipEntry objects. Please see the rubyzip documentation for details.
# File lib/zip-container/container.rb, line 421
Extracts the specified entry of the ZipContainer file to
dest_path
.
See the rubyzip documentation for details of the
on_exists_proc
parameter.
# File lib/zip-container/container.rb, line 431
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.
# File lib/zip-container/container.rb, line 238 def file @fs_file end
Searches for entries within the ZipContainer file with the specified name.
Returns nil
if no entry is found. See also
get_entry
.
# File lib/zip-container/container.rb, line 439
Searches for an entry within the ZipContainer file in a similar manner to
find_entry
, but throws +Errno::ENOENT+ if no entry is found.
# File lib/zip-container/container.rb, line 447
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.
# File lib/zip-container/container.rb, line 457
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.
# File lib/zip-container/container.rb, line 252 def get_output_stream(entry, permission = nil, &block) if reserved_entry?(entry) || managed_directory?(entry) raise ReservedNameClashError.new(entry.to_s) end @zipfile.get_output_stream(entry, permission, &block) end
Searches for entries within the ZipContainer file that match the given glob.
See the rubyzip documentation for details of the parameters that can be passed in.
# File lib/zip-container/container.rb, line 469
Is this ZipContainer file memory resident as opposed to stored on disk?
# File lib/zip-container/container.rb, line 264 def in_memory? !@on_disk 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.
# File lib/zip-container/container.rb, line 278 def mkdir(name, permission = 0755) if reserved_entry?(name) || managed_file?(name) raise ReservedNameClashError.new(name) end @zipfile.mkdir(name, permission) end
Returns the filename of this ZipContainer file.
# File lib/zip-container/container.rb, line 476
Is this ZipContainer file stored on disk as opposed to memory resident?
# File lib/zip-container/container.rb, line 290 def on_disk? @on_disk end
Returns a string containing the contents of the specified entry.
# File lib/zip-container/container.rb, line 483
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.
# File lib/zip-container/container.rb, line 300 def remove(entry) return if reserved_entry?(entry) @zipfile.remove(entry) 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.
# File lib/zip-container/container.rb, line 315 def rename(entry, new_name, &continue_on_exists_proc) return if reserved_entry?(entry) raise ReservedNameClashError.new(new_name) if reserved_entry?(new_name) @zipfile.rename(entry, new_name, &continue_on_exists_proc) 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.
# File lib/zip-container/container.rb, line 329 def replace(entry, src_path) return if reserved_entry?(entry) @zipfile.replace(entry, src_path) end
Returns the number of entries in the ZipContainer file.
# File lib/zip-container/container.rb, line 490
Return a textual summary of this ZipContainer file.
# File lib/zip-container/container.rb, line 338 def to_s @zipfile.to_s + " - #{@mimetype}" end
Verify the contents of this ZipContainer file. All managed files and directories are checked to make sure that they exist, if required.
# File lib/zip-container/container.rb, line 347 def verify! verify_managed_entries! end