module MultiZip::Backend::Zipruby
Constants
- BUFFER_SIZE
Public Instance Methods
extract_member(member_path, destination_path, options = {})
click to toggle source
# File lib/multi_zip/backend/zipruby.rb, line 34 def extract_member(member_path, destination_path, options = {}) # detect if called asked for a directory instead of a file member_not_found!(member_path) if member_path =~ /\/$/ read_operation do |ar| exists_in_archive!(ar, member_path) output_file = ::File.new(destination_path, 'wb') ar.fopen(member_path) do |member| while chunk = member.read(BUFFER_SIZE) output_file.write chunk end end output_file.close end destination_path end
list_members(prefix=nil, options={})
click to toggle source
# File lib/multi_zip/backend/zipruby.rb, line 12 def list_members(prefix=nil, options={}) read_operation do |zip| list = [] zip.num_files.times do |i| list << zip.get_name(i) end list.select{|n| prefix ? n =~ /^#{prefix}/ : true}.sort end end
member_info(member_path, options = {})
click to toggle source
# File lib/multi_zip/backend/zipruby.rb, line 61 def member_info(member_path, options = {}) read_operation do |zip| member_location = zip.locate_name(member_path) if member_location == -1 zip.close member_not_found!(member_path) end member_stats = zip.get_stat(member_location) { :path => member_stats.name, :size => member_stats.size.to_i, :type => member_stats.directory? ? :directory : :file, :original => member_stats } end end
read_member(member_path, options = {})
click to toggle source
# File lib/multi_zip/backend/zipruby.rb, line 3 def read_member(member_path, options = {}) # detect if called asked for a directory instead of a file member_not_found!(member_path) if member_path =~ /\/$/ read_operation do |ar| exists_in_archive!(ar, member_path) ar.fopen(member_path) {|member| member.read} end end
remove_member(member_path, options = {})
click to toggle source
# File lib/multi_zip/backend/zipruby.rb, line 52 def remove_member(member_path, options = {}) archive_exists! Zip::Archive.open(@filename) do |ar| exists_in_archive!(ar, member_path) ar.fdelete(ar.locate_name(member_path)) end true end
write_member(member_path, member_contents, options = {})
click to toggle source
# File lib/multi_zip/backend/zipruby.rb, line 22 def write_member(member_path, member_contents, options = {}) flags = (File.exists?(@filename) ? nil : Zip::CREATE) Zip::Archive.open(@filename, flags) do |ar| if ar.map(&:name).include?(member_path) ar.replace_buffer(member_path, member_contents) else ar.add_buffer(member_path, member_contents) end end true end
Private Instance Methods
exists_in_archive!(zip, member_path)
click to toggle source
# File lib/multi_zip/backend/zipruby.rb, line 93 def exists_in_archive!(zip, member_path) unless exists_in_archive?(zip, member_path) zip.close raise member_not_found!(member_path) end end
exists_in_archive?(zip, member_path)
click to toggle source
NOTE: Zip::Archive#locate_name return values -1 if path not found 0 if path is a directory 2 if path is a file
for a directory to be found it must include the trailing slash ('/').
# File lib/multi_zip/backend/zipruby.rb, line 89 def exists_in_archive?(zip, member_path) zip.locate_name(member_path).to_i >= 0 # will find files or dirs. end
read_operation() { |ar| ... }
click to toggle source
# File lib/multi_zip/backend/zipruby.rb, line 100 def read_operation(&blk) archive_exists! Zip::Archive.open(@filename) do |ar| yield(ar) end rescue Zip::Error => e # not the best way to detect the class of error. if e.message.match('Not a zip archive') raise MultiZip::InvalidArchiveError.new(@filename, e) else raise end end