Class: MultiZip
- Inherits:
-
Object
- Object
- MultiZip
- Defined in:
- lib/multi_zip.rb,
lib/multi_zip/errors.rb,
lib/multi_zip/version.rb
Defined Under Namespace
Modules: Backend Classes: ArchiveError, ArchiveNotFoundError, BaseError, InvalidArchiveError, InvalidBackendError, MemberError, MemberNotFoundError, NoSupportedBackendError
Constant Summary collapse
- BACKEND_PREFERENCE =
[ :rubyzip, :archive_zip, :zipruby ]
- BACKENDS =
{ :rubyzip => { :fingerprints => [ ['constant', lambda { defined?(Zip::File) } ], [nil, lambda { defined?(Zip::Archive) }] ], :constant => lambda { MultiZip::Backend::Rubyzip } }, :archive_zip => { :fingerprints => [ ['constant', lambda { defined?(Archive::Zip) } ] ], :constant => lambda { MultiZip::Backend::ArchiveZip } }, :zipruby => { :fingerprints => [ ['constant', lambda { defined?(Zip::File) } ], ['constant', lambda { defined?(Zip::Archive) }] ], :constant => lambda { MultiZip::Backend::Zipruby } } }
- VERSION =
"0.1.4"
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
Class Method Summary collapse
Instance Method Summary collapse
- #backend ⇒ Object
- #backend=(backend_name) ⇒ Object
-
#close ⇒ Object
Close the archive, if the archive is open.
-
#extract_member(member_path, destination_path, options = {}) ⇒ Object
Intended to write the contents of a zip member to a filesystem path.
-
#initialize(filename, options = {}) ⇒ MultiZip
constructor
A new instance of MultiZip.
-
#list_members(prefix = nil, options = {}) ⇒ Object
List members of the zip file.
-
#member_exists?(member_path, options = {}) ⇒ Boolean
Boolean, does a given member path exist in the zip file?.
-
#read_member(member_path, options = {}) ⇒ Object
Intended to return the contents of a zip member as a string.
-
#read_members(member_paths, options = {}) ⇒ Object
Intended to return the contents of zip members as array of strings.
-
#remove_member(member_path, options = {}) ⇒ Object
Remove a zip member from the archive.
-
#remove_members(member_paths, options = {}) ⇒ Object
Remove multiple zip member from the archive.
-
#write_member(member_path, member_content, options = {}) ⇒ Object
Write string contents to a zip member file.
Constructor Details
#initialize(filename, options = {}) ⇒ MultiZip
Returns a new instance of MultiZip.
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/multi_zip.rb', line 30 def initialize(filename, = {}) @filename = filename self.backend = if b_end = .delete(:backend) b_end else default_backend end if block_given? yield(self) end end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
4 5 6 |
# File 'lib/multi_zip.rb', line 4 def filename @filename end |
Class Method Details
.available_backends ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/multi_zip.rb', line 64 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 |
.supported_backends ⇒ Object
60 61 62 |
# File 'lib/multi_zip.rb', line 60 def self.supported_backends BACKENDS.keys end |
Instance Method Details
#backend ⇒ Object
44 45 46 |
# File 'lib/multi_zip.rb', line 44 def backend @backend ||= default_backend end |
#backend=(backend_name) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/multi_zip.rb', line 48 def backend=(backend_name) return 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 ⇒ Object
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.
83 84 85 |
# File 'lib/multi_zip.rb', line 83 def close return true end |
#extract_member(member_path, destination_path, options = {}) ⇒ Object
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.
107 108 109 110 |
# File 'lib/multi_zip.rb', line 107 def extract_member(member_path, destination_path, ={}) warn "Using default #extract_member which may be memory-inefficient" default_extract_member(member_path, destination_path, ) end |
#list_members(prefix = nil, options = {}) ⇒ Object
List members of the zip file. Optionally can specify a prefix.
This method MUST be overridden by a backend module.
115 116 117 |
# File 'lib/multi_zip.rb', line 115 def list_members(prefix = nil, ={}) raise NotImplementedError end |
#member_exists?(member_path, options = {}) ⇒ Boolean
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.
123 124 125 |
# File 'lib/multi_zip.rb', line 123 def member_exists?(member_path, ={}) list_members(nil, ).include?(member_path) end |
#read_member(member_path, options = {}) ⇒ Object
Intended to return the contents of a zip member as a string.
This method MUST be overridden by a backend module.
90 91 92 |
# File 'lib/multi_zip.rb', line 90 def read_member(member_path, ={}) raise NotImplementedError end |
#read_members(member_paths, options = {}) ⇒ Object
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.
98 99 100 |
# File 'lib/multi_zip.rb', line 98 def read_members(member_paths, ={}) member_paths.map{|f| read_member(f, ) } end |
#remove_member(member_path, options = {}) ⇒ Object
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.
137 138 139 |
# File 'lib/multi_zip.rb', line 137 def remove_member(member_path, ={}) raise NotImplementedError end |
#remove_members(member_paths, options = {}) ⇒ Object
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.
147 148 149 |
# File 'lib/multi_zip.rb', line 147 def remove_members(member_paths, ={}) member_paths.map{|f| remove_member(f, ) }.all? end |
#write_member(member_path, member_content, options = {}) ⇒ Object
Write string contents to a zip member file
128 129 130 |
# File 'lib/multi_zip.rb', line 128 def write_member(member_path, member_content, ={}) raise NotImplementedError end |