Class: Zippo::ZipFile
- Inherits:
-
Object
- Object
- Zippo::ZipFile
- Extended by:
- Forwardable
- Defined in:
- lib/zippo/zip_file.rb
Overview
ZipFile represents a Zip archive.
It can be called in block form like this:
Zippo.open("file.zip") do |zip|
str = zip["file.txt"]
other = zip["other/file.txt"]
puts str
end
Or without a block:
zip = Zippo.open("file.zip")
puts zip["file.txt"]
zip.close
Inserting archive members
New archive members can be inserted using the insert method:
zip = Zippo.open("out.zip", "w")
zip.insert "out.txt", "something.txt"
Additionally Zipfile#[]= has been overridden to support inserting strings directly:
zip["other.txt"] = "now is the time"
If you already have an IO object, you can just insert it:
io = File.open("foo.bin")
zip.insert "rename.bin", io
Finally, you can insert a Zippo::ZipMember from another Zip file, which can allow the compressed data to be copied directly (avoiding having to uncompress and then recompress it):
other = Zippo.open("other.zip")
zip.insert "final.bin", other["final.bin"]
No data will actually be written until the ZipFile is closed:
zip.close
Class Method Summary collapse
-
.open(filename, mode = 'r') ⇒ Zippo::ZipFile
Opens a zip archive file.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the ZipFile, writing it to disk if it was opened in write mode.
-
#directory ⇒ ZipDirectory
The ZipDirectory.
-
#initialize(filename, mode) ⇒ ZipFile
constructor
A new instance of ZipFile.
Constructor Details
#initialize(filename, mode) ⇒ ZipFile
Returns a new instance of ZipFile.
68 69 70 71 |
# File 'lib/zippo/zip_file.rb', line 68 def initialize(filename, mode) @filename = filename @mode = mode end |
Class Method Details
.open(filename, mode = 'r') ⇒ Zippo::ZipFile
Opens a zip archive file
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/zippo/zip_file.rb', line 57 def self.open(filename, mode = 'r') if block_given? zippo = new(filename, mode) a = yield zippo zippo.close return a else new filename, mode end end |
Instance Method Details
#close ⇒ Object
Closes the ZipFile, writing it to disk if it was opened in write mode
78 79 80 81 82 83 84 85 86 |
# File 'lib/zippo/zip_file.rb', line 78 def close # XXX should optimize to not write anything to unchanged files # In update mode, we first write the zip to a temporary zip file, # then move it on top of the original file out_zip = update? ? tmp_zipfile : @filename ZipFileWriter.new(directory, out_zip).write if write? @io.close if @io File.rename out_zip, @filename if update? end |
#directory ⇒ ZipDirectory
Returns the ZipDirectory.
89 90 91 92 93 94 95 96 |
# File 'lib/zippo/zip_file.rb', line 89 def directory @directory ||= if read? ZipDirectory.new io else ZipDirectory.new end end |