lib/zip-container/file.rb in zip-container-2.0.0 vs lib/zip-container/file.rb in zip-container-2.1.0

- old
+ new

@@ -43,40 +43,24 @@ # class in the rubyzip gem. Please also consult the # {rubyzip documentation}[http://rubydoc.info/gems/rubyzip/1.1.0/frames] # alongside these pages. # # There are code examples available with the source code of this library. - class File - include ReservedNames - include ManagedEntries + class File < Container extend Forwardable - def_delegators :@zipfile, :comment, :comment=, :commit_required?, :each, + def_delegators :@container, :comment, :comment=, :commit_required?, :each, :entries, :extract, :get_input_stream, :name, :read, :size private_class_method :new - # The mime-type of this ZipContainer file. - attr_reader :mimetype - # :stopdoc: - # The reserved mimetype file name for standard ZipContainer documents. - MIMETYPE_FILE = "mimetype" + def initialize(location) + super(location) - def initialize(document) - @zipfile = open_document(document) - check_mimetype! - - @mimetype = read_mimetype @on_disk = true - # Reserved entry names. Just the mimetype file by default. - register_reserved_name(MIMETYPE_FILE) - - # Initialize the managed entry tables. - initialize_managed_entries - # Here we fake up the connection to the rubyzip filesystem classes so # that they also respect the reserved names that we define. mapped_zip = ::Zip::FileSystem::ZipFileNameMapper.new(self) @fs_dir = ::Zip::FileSystem::ZipFsDir.new(mapped_zip) @fs_file = ::Zip::FileSystem::ZipFsFile.new(mapped_zip) @@ -130,57 +114,10 @@ c.each end # :call-seq: - # File.open(filename) -> document - # File.open(filename) {|document| ...} - # - # Open an existing ZipContainer file from disk. It will be checked for - # conformance upon first access. - def self.open(filename, &block) - c = new(filename) - - if block_given? - begin - yield c - ensure - c.close - end - end - - c - end - - # :call-seq: - # File.verify(filename) -> boolean - # - # 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). - def self.verify(filename) - begin - new(filename).verify! - rescue - return false - end - - true - end - - # :call-seq: - # File.verify!(filename) - # - # 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). - def self.verify!(filename) - new(filename).verify! - end - - # :call-seq: # add(entry, src_path, &continue_on_exists_proc) # # 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 @@ -191,11 +128,11 @@ 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) + @container.add(entry, src_path, &continue_on_exists_proc) end # :call-seq: # commit -> boolean # close -> boolean @@ -205,11 +142,11 @@ # otherwise. def commit return false unless commit_required? if on_disk? - @zipfile.commit + @container.commit end end alias :close :commit @@ -249,11 +186,11 @@ unless options[:include_hidden] return if hidden_entry?(entry_name) end - @zipfile.find_entry(entry_name) + @container.find_entry(entry_name) end # :call-seq: # get_entry(entry, options = {}) -> Zip::Entry or nil # @@ -266,11 +203,11 @@ unless options[:include_hidden] raise Errno::ENOENT, entry if hidden_entry?(entry) end - @zipfile.get_entry(entry) + @container.get_entry(entry) end # :call-seq: # get_output_stream(entry, permission = nil) -> stream # get_output_stream(entry, permission = nil) {|stream| ...} @@ -284,11 +221,11 @@ 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) + @container.get_output_stream(entry, permission, &block) end # :call-seq: # glob(pattern) -> Array # glob(pattern) { |entry| ... } @@ -345,11 +282,11 @@ def mkdir(name, permission = 0755) if reserved_entry?(name) || managed_file?(name) raise ReservedNameClashError.new(name) end - @zipfile.mkdir(name, permission) + @container.mkdir(name, permission) end # :call-seq: # on_disk? -> boolean # @@ -364,11 +301,11 @@ # 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. def remove(entry) return if reserved_entry?(entry) - @zipfile.remove(entry) + @container.remove(entry) end # :call-seq: # rename(entry, new_name, &continue_on_exists_proc) # @@ -381,11 +318,11 @@ # +continue_on_exists_proc+ parameter. 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) + @container.rename(entry, new_name, &continue_on_exists_proc) end # :call-seq: # replace(entry, src_path) # @@ -393,52 +330,41 @@ # 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. def replace(entry, src_path) return if reserved_entry?(entry) - @zipfile.replace(entry, src_path) + @container.replace(entry, src_path) end # :call-seq: # to_s -> String # # Return a textual summary of this ZipContainer file. def to_s - @zipfile.to_s + " - #{@mimetype}" + @container.to_s + " - #{@mimetype}" end - # :call-seq: - # verify! - # - # Verify the contents of this ZipContainer file. All managed files and - # directories are checked to make sure that they exist, if required. - def verify! - verify_managed_entries! - end - private - def open_document(document) + def open_container(document) ::Zip::File.new(document) end - def check_mimetype! + def verify_mimetype # Check mimetype file is present and correct. - entry = @zipfile.find_entry(MIMETYPE_FILE) + entry = @container.find_entry(MIMETYPE_FILE) - raise MalformedContainerError.new("'mimetype' file is missing.") if entry.nil? + return "'mimetype' file is missing." if entry.nil? if entry.local_header_offset != 0 - raise MalformedContainerError.new("'mimetype' file is not at offset 0 in the archive.") + return "'mimetype' file is not at offset 0 in the archive." end if entry.compression_method != ::Zip::Entry::STORED - raise MalformedContainerError.new("'mimetype' file is compressed.") + return "'mimetype' file is compressed." end - - true end def read_mimetype - @zipfile.read(MIMETYPE_FILE) + @container.read(MIMETYPE_FILE) end public # Lots of extra docs out of the way at the end here...