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 Takes a hash of options and entries.
-
#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 Takes a hash of options and entries. Options use symbols and entries use strings. The :filename option is optional; if none is provided, a generated, temporary filename will be used.
Example: Zippy.new(:filename => ‘my.zip’, ‘README’ => ‘Thank you for reading me.’)
16 17 18 19 20 21 22 23 |
# File 'lib/zippy.rb', line 16 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 zipfile.commit if autocommit? yield self if block_given? end |
Class Method Details
.[](filename, entry = nil) ⇒ Object
177 178 179 |
# File 'lib/zippy.rb', line 177 def self.[](filename, entry=nil) entry ? read(filename, entry) : list(filename) end |
.[]=(filename, entry, content) ⇒ Object
181 182 183 |
# File 'lib/zippy.rb', line 181 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
131 132 133 134 135 136 |
# File 'lib/zippy.rb', line 131 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
151 152 153 154 155 156 157 |
# File 'lib/zippy.rb', line 151 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’]
162 163 164 165 166 |
# File 'lib/zippy.rb', line 162 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
140 141 142 143 144 145 146 147 148 |
# File 'lib/zippy.rb', line 140 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
170 171 172 173 174 |
# File 'lib/zippy.rb', line 170 def self.read(filename, entry) content = nil open(filename){|z| content = z[entry] } content end |
Instance Method Details
#[](entry) ⇒ Object
Read an entry
45 46 47 48 |
# File 'lib/zippy.rb', line 45 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
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/zippy.rb', line 52 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
87 88 89 90 |
# File 'lib/zippy.rb', line 87 def close write(filename) zipfile.close end |
#data ⇒ Object
Returns the entire archive as a string
105 106 107 108 109 |
# File 'lib/zippy.rb', line 105 def data return nil if empty? zipfile.commit File.read(filename, :encoding => Encoding::BINARY) end |
#delete(*entries) ⇒ Object
Delete an entry
70 71 72 73 74 75 76 |
# File 'lib/zippy.rb', line 70 def delete(*entries) entries.each do |entry| zipfile.remove(entry) end zipfile.commit if autocommit? entries end |
#each ⇒ Object
26 27 28 |
# File 'lib/zippy.rb', line 26 def each zipfile.each{|e| yield e.name } end |
#empty? ⇒ Boolean
34 35 36 |
# File 'lib/zippy.rb', line 34 def empty? size.zero? end |
#filename ⇒ Object
112 113 114 |
# File 'lib/zippy.rb', line 112 def filename @filename ||= random_filename end |
#filename=(filename) ⇒ Object
116 117 118 119 |
# File 'lib/zippy.rb', line 116 def filename=(filename) rename_file(filename) @filename = filename end |
#paths ⇒ Object
Returns the full path to all entries in the archive
39 40 41 |
# File 'lib/zippy.rb', line 39 def paths map{|p| p } end |
#rename(old_name, new_name) ⇒ Object
Rename an entry
79 80 81 82 83 |
# File 'lib/zippy.rb', line 79 def rename(old_name, new_name) zipfile.rename(old_name, new_name) zipfile.commit if autocommit? old_name end |
#size ⇒ Object
30 31 32 |
# File 'lib/zippy.rb', line 30 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)
95 96 97 98 99 100 101 102 |
# File 'lib/zippy.rb', line 95 def write(filename) return false if empty? zipfile.commit unless filename == self.filename FileUtils.cp(self.filename, filename) end true end |
#zipfile ⇒ Object
122 123 124 |
# File 'lib/zippy.rb', line 122 def zipfile @zipfile ||= Zip::File.new(filename, true) end |