class MultiZip
Constants
- BACKENDS
- BACKEND_PREFERENCE
- VERSION
Attributes
Public Class Methods
# File lib/multi_zip.rb, line 78 def self.available_backends available = [] BACKENDS.each do |name, opts| if opts[:fingerprints].all?{|expectation, lmb| lmb.call == expectation } available << name end end available end
# File lib/multi_zip.rb, line 36 def initialize(filename, options = {}) @filename = filename self.backend = if b_end = options.delete(:backend) b_end else begin default_backend rescue NoSupportedBackendError => e if !!options[:allow_no_backends] # TODO: Fix this test mode hack nil else raise e end end end if block_given? yield(self) end end
# File lib/multi_zip.rb, line 74 def self.supported_backends BACKENDS.keys end
Public Instance Methods
# File lib/multi_zip.rb, line 58 def backend @backend ||= default_backend end
# File lib/multi_zip.rb, line 62 def backend=(backend_name) return @backend if backend_name.nil? if BACKENDS.keys.include?(backend_name.to_sym) @backend = backend_name.to_sym require "multi_zip/backend/#{@backend}" extend BACKENDS[@backend][:constant].call return @backend else raise InvalidBackendError.new(backend_name) end end
Close the archive, if the archive is open. If the archive is already closed, behave as though it was open. Expected to always return true.
This is currently a non-op since the archives are not kept open between method calls. It is here so users can write code using it to prepare for when we do keep the archives open.
Currently, this method MUST NOT be overridden.
# File lib/multi_zip.rb, line 97 def close return true end
Intended to write the contents of a zip member to a filesystem path.
This SHOULD be overridden by a backend module because this default will try to read the whole file in to memory before outputting to disk and that can be memory-intensive if the file is large.
# File lib/multi_zip.rb, line 121 def extract_member(member_path, destination_path, options={}) warn "Using default #extract_member which may be memory-inefficient" default_extract_member(member_path, destination_path, options) end
List members of the zip file. Optionally can specify a prefix.
This method MUST be overridden by a backend module.
# File lib/multi_zip.rb, line 129 def list_members(prefix = nil, options={}) raise NotImplementedError end
Boolean, does a given member path exist in the zip file?
This method MAY be overridden by backend module for the sake of efficiency. Otherwise it will use list_members
.
# File lib/multi_zip.rb, line 137 def member_exists?(member_path, options={}) list_members(nil, options).include?(member_path) end
Returns a hash of information about the member file. At a minimum, the hash MUST contain these keys:
:path the full path of the member file (should equal `member_path` arg) :size the UNCOMPRESSED file size, in bytes
Optionally, it MAY contain these keys:
:created_at creation timestamp of the file as an instance of Time :compressed_size size of the COMPRESSED file :type Filesystem type of the member (:directory, :file, :symlink, etc) :original The original member info object as returned from the backend gem. Ex: using rubyzip, it would be an instace of Zip::Entry.
This method MUST be overridden by a backend module.
# File lib/multi_zip.rb, line 154 def member_info(member_path, options={}) raise NotImplementedError end
Intended to return the contents of a zip member as a string.
This method MUST be overridden by a backend module.
# File lib/multi_zip.rb, line 104 def read_member(member_path, options={}) raise NotImplementedError end
Intended to return the contents of zip members as array of strings.
This method MAY be overridden by backend module for the sake of efficiency, or will call read_member
for each entry in member_paths.
# File lib/multi_zip.rb, line 112 def read_members(member_paths, options={}) member_paths.map{|f| read_member(f, options) } end
Remove a zip member from the archive. Expected to raise MemberNotFoundError
if the member_path was not found in the archive
This method MUST be overridden by a backend module.
# File lib/multi_zip.rb, line 168 def remove_member(member_path, options={}) raise NotImplementedError end
Remove multiple zip member from the archive. Expected to raise MemberNotFoundError
if the member_path was not found in the archive
This method MAY be overridden by backend module for the sake of efficiency. Otherwise it will use remove_member
.
# File lib/multi_zip.rb, line 178 def remove_members(member_paths, options={}) member_paths.map{|f| remove_member(f, options) }.all? end
Write string contents to a zip member file
# File lib/multi_zip.rb, line 159 def write_member(member_path, member_content, options={}) raise NotImplementedError end
Private Instance Methods
Convenience method that will raise ArchiveNotFoundError
if the archive doesn't exist or if the archive path given points to something other than a file.
# File lib/multi_zip.rb, line 187 def archive_exists! unless File.file?(@filename) raise ArchiveNotFoundError.new(@filename) end end
# File lib/multi_zip.rb, line 214 def default_backend BACKEND_PREFERENCE.each do |name| be = BACKENDS[name] if be[:fingerprints].all?{|expectation, lmb| lmb.call == expectation } return name end end raise NoSupportedBackendError end
# File lib/multi_zip.rb, line 207 def default_extract_member(member_path, destination_path, options={}) output_file = ::File.new(destination_path, 'wb') output_file.write(read_member(member_path, options)) output_file.close destination_path end
Convenience method that will raise MemberNotFoundError
if the member doesn't exist. Uses member_exists?
in whatever form (default or custom).
# File lib/multi_zip.rb, line 195 def exists!(member_path) unless member_exists?(member_path) member_not_found!(member_path) end true end
Raises MemberNotFoundError
# File lib/multi_zip.rb, line 203 def member_not_found!(member_path) raise MemberNotFoundError.new(member_path) end