Class: Zippy
Class Method Summary collapse
- .[](filename, entry = nil) ⇒ Object
- .[]=(filename, entry, content) ⇒ Object
-
.create(filename, options_and_entries = {}, &b) ⇒ Object
Create a new archive with the name
filename
, populate it and then close it. -
.each(filename) ⇒ Object
Iterate each entry name and its contents in the archive
filename
. -
.list(filename) ⇒ Object
Returns an array of entry names from the archive
filename
. -
.open(filename, options_and_entries = {}) ⇒ Object
Works the same as new, but require’s an explicit filename If a block is provided, it will be closed at the end of the block.
-
.read(filename, entry) ⇒ Object
Read the contents of a single entry in
filename
.
Instance Method Summary collapse
-
#[](entry) ⇒ Object
Read an entry.
-
#[]=(entry, contents) ⇒ Object
Add or change an entry with the name
entry
contents
can be a string or an IO. -
#close ⇒ Object
Close the archive for writing.
-
#data ⇒ Object
Returns the entire archive as a string.
-
#delete(*entries) ⇒ Object
Delete an entry.
- #each ⇒ Object
- #empty? ⇒ Boolean
- #filename ⇒ Object
- #filename=(filename) ⇒ Object
-
#initialize(entries_and_options = {}) {|_self| ... } ⇒ Zippy
constructor
Make an archive Filename is optional; if none is provided, a generated, temporary filename will be used.
-
#paths ⇒ Object
Returns the full path to all entries in the archive.
-
#rename(old_name, new_name) ⇒ Object
Rename an entry.
- #size ⇒ Object
-
#write(filename) ⇒ Object
Write the archive to
filename
If a filename is not provided, it will write to the default filename (self.filename). - #zipfile ⇒ Object
Constructor Details
#initialize(entries_and_options = {}) {|_self| ... } ⇒ Zippy
Make an archive Filename is optional; if none is provided, a generated, temporary filename will be used. If a block is provided, the archive is yielded Takes an optional second parameter which is a hash of entries that will be added after initialization
15 16 17 18 19 20 21 |
# File 'lib/zippy.rb', line 15 def initialize(={}) .each{|k,v| send("#{k}=", v) if respond_to?("#{k}=") && k.is_a?(Symbol) } without_autocommit do .each{|k,v| self[k] = v if k.is_a?(String) } end yield self if block_given? end |
Class Method Details
.[](filename, entry = nil) ⇒ Object
175 176 177 |
# File 'lib/zippy.rb', line 175 def self.[](filename, entry=nil) entry ? read(filename, entry) : list(filename) end |
.[]=(filename, entry, content) ⇒ Object
179 180 181 |
# File 'lib/zippy.rb', line 179 def self.[]=(filename, entry, content) open(filename){|z| z[entry] = content } end |
.create(filename, options_and_entries = {}, &b) ⇒ Object
Create a new archive with the name filename
, populate it and then close it
Warning: Will overwrite existing file
129 130 131 132 133 134 |
# File 'lib/zippy.rb', line 129 def self.create(filename, ={}, &b) File.unlink(filename) if File.exists?(filename) z = new({:filename => filename}.merge(), &b) z.close z end |
.each(filename) ⇒ Object
Iterate each entry name and its contents in the archive filename
149 150 151 152 153 154 155 |
# File 'lib/zippy.rb', line 149 def self.each(filename) open(filename) do |zip| zip.each do |name| yield name, zip[name] end end end |
.list(filename) ⇒ Object
Returns an array of entry names from the archive filename
Zippy.list(‘my.zip’) #=> [‘foo’, ‘bar’]
160 161 162 163 164 |
# File 'lib/zippy.rb', line 160 def self.list(filename) list = nil open(filename){|z| list = z.paths } list end |
.open(filename, options_and_entries = {}) ⇒ Object
Works the same as new, but require’s an explicit filename If a block is provided, it will be closed at the end of the block
138 139 140 141 142 143 144 145 146 |
# File 'lib/zippy.rb', line 138 def self.open(filename, ={}) raise(ArgumentError, "file \"#{filename}\" does not exist") unless File.exists?(filename) z = new({:filename => filename}.merge()) if block_given? yield z z.close end z end |
.read(filename, entry) ⇒ Object
Read the contents of a single entry in filename
168 169 170 171 172 |
# File 'lib/zippy.rb', line 168 def self.read(filename, entry) content = nil open(filename){|z| content = z[entry] } content end |
Instance Method Details
#[](entry) ⇒ Object
Read an entry
43 44 45 46 |
# File 'lib/zippy.rb', line 43 def [](entry) return nil unless include?(entry) zipfile.read(entry) end |
#[]=(entry, contents) ⇒ Object
Add or change an entry with the name entry
contents
can be a string or an IO
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/zippy.rb', line 50 def []=(entry, contents) zipfile.get_output_stream entry do |s| if contents.is_a?(String) s.write contents elsif contents.respond_to?(:read) s.write contents.read(1024) until contents.eof? elsif contents.respond_to?(:to_s) s.write contents.to_s else#Not sure these last two are different s.write "#{contents}" end end zipfile.commit if autocommit? true end |
#close ⇒ Object
Close the archive for writing
85 86 87 88 |
# File 'lib/zippy.rb', line 85 def close write(filename) zipfile.close end |
#data ⇒ Object
Returns the entire archive as a string
103 104 105 106 107 |
# File 'lib/zippy.rb', line 103 def data return nil if empty? zipfile.commit File.read(filename) end |
#delete(*entries) ⇒ Object
Delete an entry
68 69 70 71 72 73 74 |
# File 'lib/zippy.rb', line 68 def delete(*entries) entries.each do |entry| zipfile.remove(entry) end zipfile.commit if autocommit? entries end |
#each ⇒ Object
24 25 26 |
# File 'lib/zippy.rb', line 24 def each zipfile.each{|e| yield e.name } end |
#empty? ⇒ Boolean
32 33 34 |
# File 'lib/zippy.rb', line 32 def empty? size.zero? end |
#filename ⇒ Object
110 111 112 |
# File 'lib/zippy.rb', line 110 def filename @filename ||= random_filename end |
#filename=(filename) ⇒ Object
114 115 116 117 |
# File 'lib/zippy.rb', line 114 def filename=(filename) rename_file(filename) @filename = filename end |
#paths ⇒ Object
Returns the full path to all entries in the archive
37 38 39 |
# File 'lib/zippy.rb', line 37 def paths map end |
#rename(old_name, new_name) ⇒ Object
Rename an entry
77 78 79 80 81 |
# File 'lib/zippy.rb', line 77 def rename(old_name, new_name) zipfile.rename(old_name, new_name) zipfile.commit if autocommit? old_name end |
#size ⇒ Object
28 29 30 |
# File 'lib/zippy.rb', line 28 def size zipfile.size end |
#write(filename) ⇒ Object
Write the archive to filename
If a filename is not provided, it will write to the default filename (self.filename)
93 94 95 96 97 98 99 100 |
# File 'lib/zippy.rb', line 93 def write(filename) return false if empty? zipfile.commit unless filename == self.filename FileUtils.cp(self.filename, filename) end true end |
#zipfile ⇒ Object
120 121 122 |
# File 'lib/zippy.rb', line 120 def zipfile @zipfile ||= Zip::ZipFile.new(filename, true) end |