Module: MultiZip::Backend::Zipruby

Defined in:
lib/multi_zip/backend/zipruby.rb

Constant Summary collapse

BUFFER_SIZE =
8192

Instance Method Summary collapse

Instance Method Details

#extract_member(member_path, destination_path, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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 = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
# 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

#read_member(member_path, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
# 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 = {}) ⇒ Object



52
53
54
55
56
57
58
59
# 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 = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# 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