This class represents a UCF document in PK Zip format. See the specification 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 UCF document. By default this is “application/epub+zip”.
Create a new UCF document on disk with the specified mimetype.
# File lib/ucf/container.rb, line 90 def Container.create(filename, mimetype = DEFAULT_MIMETYPE, &block) ::Zip::ZipOutputStream.open(filename) do |stream| stream.put_next_entry(MIMETYPE_FILE, nil, nil, ::Zip::ZipEntry::STORED) stream.write mimetype end Container.open(filename, &block) end
Iterate over the entries in the UCF document. The entry objects returned by this method are Zip::ZipEntry objects. Please see the rubyzip documentation for details.
# File lib/ucf/container.rb, line 106 def Container.each_entry(filename, &block) c = new(filename) if block_given? begin c.each(&block) ensure c.close end end c.each end
Verify that the specified UCF document conforms
to the UCF specification. This method returns
false
if there are any problems at all with the file
(including if it cannot be found).
# File lib/ucf/container.rb, line 146 def Container.verify(filename) begin Container.verify!(filename) rescue return false end true end
Verify that the specified UCF document conforms to the UCF 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/ucf/container.rb, line 163 def Container.verify!(filename) new(filename).close nil end
Convenience method for adding the contents of a file to the UCF document. 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/ucf/container.rb, line 178 def add(entry, src_path, &continue_on_exists_proc) raise ReservedNameClashError.new(entry.to_s) if reserved_entry?(entry) @zipfile.add(entry, src_path, &continue_on_exists_proc) end
Returns the UCF document comment, if it has one.
# File lib/ucf/container.rb, line 400
Set the UCF document comment to the new value.
# File lib/ucf/container.rb, line 407
Commits changes that have been made since the previous commit to the UCF document. Returns true
if anything
was actually done, false
otherwise.
# File lib/ucf/container.rb, line 191 def commit return false unless commit_required? if on_disk? @zipfile.commit end end
Returns true
if any changes have been made to this UCF document since the last commit,
false
otherwise.
# File lib/ucf/container.rb, line 415
Returns an object which can be used like ruby’s built in Dir
(class) object, except that it works on the UCF
document on which this method is invoked.
See the rubyzip documentation for details.
# File lib/ucf/container.rb, line 209 def dir @fs_dir end
Iterate over the entries in the UCF document. The entry objects returned by this method are Zip::ZipEntry objects. Please see the rubyzip documentation for details.
# File lib/ucf/container.rb, line 425
Extracts the specified entry of the UCF document
to dest_path
.
See the rubyzip documentation for details of the
on_exists_proc
parameter.
# File lib/ucf/container.rb, line 435
Returns an object which can be used like ruby’s built in File
(class) object, except that it works on the UCF
document on which this method is invoked.
See the rubyzip documentation for details.
# File lib/ucf/container.rb, line 221 def file @fs_file end
Searches for entries within the UCF document with
the specified name. Returns nil
if no entry is found. See also
get_entry
.
# File lib/ucf/container.rb, line 443
Searches for an entry within the UCF document in
a similar manner to find_entry
, but throws +Errno::ENOENT+ if
no entry is found.
# File lib/ucf/container.rb, line 451
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/ucf/container.rb, line 461
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/ucf/container.rb, line 235 def get_output_stream(entry, permission = nil, &block) raise ReservedNameClashError.new(entry.to_s) if reserved_entry?(entry) @zipfile.get_output_stream(entry, permission, &block) end
Searches for entries within the UCF document that match the given glob.
See the rubyzip documentation for details of the parameters that can be passed in.
# File lib/ucf/container.rb, line 472
Is this UCF document memory resident as opposed to stored on disk?
# File lib/ucf/container.rb, line 245 def in_memory? !@on_disk end
Creates a directory in the UCF document. If asked to create a directory with a reserved name 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/ucf/container.rb, line 258 def mkdir(name, permission = 0755) raise ReservedNameClashError.new(name) if reserved_entry?(name) @zipfile.mkdir(name, permission) end
Returns the filename of this UCF document.
# File lib/ucf/container.rb, line 479
Is this UCF document stored on disk as opposed to memory resident?
# File lib/ucf/container.rb, line 268 def on_disk? @on_disk end
Returns a string containing the contents of the specified entry.
# File lib/ucf/container.rb, line 486
Removes the specified entry from the UCF document. If asked to remove any reserved files such as the special mimetype header file this method will do nothing.
# File lib/ucf/container.rb, line 278 def remove(entry) return if reserved_entry?(entry) @zipfile.remove(entry) end
Renames the specified entry in the UCF document. 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/ucf/container.rb, line 293 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 UCF document
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/ucf/container.rb, line 307 def replace(entry, src_path) return if reserved_entry?(entry) @zipfile.replace(entry, src_path) end
Return a list of reserved directory names for this UCF document.
When creating a more specialized sub-class of this class then this method should be overridden to add any extra reserved directory names.
# File lib/ucf/container.rb, line 330 def reserved_directories [META_INF_DIR] end
Is the given entry name in the reserved list of file or directory names?
# File lib/ucf/container.rb, line 338 def reserved_entry?(entry) name = entry.kind_of?(::Zip::ZipEntry) ? entry.name : entry name.chop! if name.end_with? "/" reserved_names.map { |n| n.downcase }.include? name.downcase end
Return a list of reserved file names for this UCF document.
When creating a more specialized sub-class of this class then this method should be overridden to add any extra reserved file names.
# File lib/ucf/container.rb, line 319 def reserved_files [MIMETYPE_FILE] end
Return a list of reserved file and directory names for this UCF document.
In practice this method simply returns the joined lists of reserved file and directory names.
# File lib/ucf/container.rb, line 352 def reserved_names reserved_files + reserved_directories end
Returns the number of entries in the UCF document.
# File lib/ucf/container.rb, line 493
Return a textual summary of this UCF document.
# File lib/ucf/container.rb, line 360 def to_s @zipfile.to_s + " - #{@mimetype}" end