class Zip::File

Zip::File is modeled after java.util.zip.ZipFile from the Java SDK. The most important methods are those for accessing information about the entries in the archive and methods such as ‘get_input_stream` and `get_output_stream` for reading from and writing entries to the archive. The class includes a few convenience methods such as `extract` for extracting entries to the filesystem, and `remove`, `replace`, `rename` and `mkdir` for making simple modifications to the archive.

Modifications to a zip archive are not committed until ‘commit` or `close` is called. The method `open` accepts a block following the pattern from ::File.open offering a simple way to automatically close the archive when the block returns.

The following example opens zip archive ‘my.zip` (creating it if it doesn’t exist) and adds an entry ‘first.txt` and a directory entry `a_dir` to it.

“‘ require ’zip’

Zip::File.open(‘my.zip’, create: true) do |zipfile|

zipfile.get_output_stream('first.txt') { |f| f.puts 'Hello from Zip::File' }
zipfile.mkdir('a_dir')

end “‘

The next example reopens ‘my.zip`, writes the contents of `first.txt` to standard out and deletes the entry from the archive.

“‘ require ’zip’

Zip::File.open(‘my.zip’, create: true) do |zipfile|

puts zipfile.read('first.txt')
zipfile.remove('first.txt')

end

Zip::FileSystem offers an alternative API that emulates ruby’s interface for accessing the filesystem, ie. the ::File and ::Dir classes.