Class Zip::ZipFile
In: lib/zip/zip.rb
lib/zip/zipfilesystem.rb
Parent: ZipCentralDirectory

ZipFile is modeled after java.util.zip.ZipFile from the Java SDK. The most important methods are those inherited from ZipCentralDirectory 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'

  Zip::ZipFile.open("my.zip", Zip::ZipFile::CREATE) {
   |zipfile|
    zipfile.get_output_stream("first.txt") { |f| f.puts "Hello from ZipFile" }
    zipfile.mkdir("a_dir")
  }

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'

  Zip::ZipFile.open("my.zip", Zip::ZipFile::CREATE) {
    |zipfile|
    puts zipfile.read("first.txt")
    zipfile.remove("first.txt")
  }

ZipFileSystem offers an alternative API that emulates ruby‘s interface for accessing the filesystem, ie. the File and Dir classes.

Methods

add   close   commit   commit_required?   extract   find_entry   foreach   get_entry   get_input_stream   get_output_stream   mkdir   new   open   read   remove   rename   replace   to_s  

Included Modules

ZipFileSystem

Constants

CREATE = 1

Attributes

comment  [RW]  Returns the zip files comment, if it has one
name  [R] 
restore_ownership  [RW]  default -> false
restore_permissions  [RW]  default -> false
restore_times  [RW]  default -> true

Public Class methods

Iterates over the contents of the ZipFile. This is more efficient than using a ZipInputStream since this methods simply iterates through the entries in the central directory structure in the archive whereas ZipInputStream jumps through the entire archive accessing the local entry headers (which contain the same information as the central directory).

Opens a zip archive. Pass true as the second parameter to create a new archive if it doesn‘t exist already.

Same as new. If a block is passed the ZipFile object is passed to the block and is automatically closed afterwards just as with ruby‘s builtin File.open method.

Public Instance methods

Convenience method for adding the contents of a file to the archive

Closes the zip file committing any changes that has been made.

Commits changes that has been made since the previous commit to the zip archive.

Returns true if any changes has been made to this archive since the previous commit

Extracts entry to file destPath.

Searches for entry with the specified name. Returns nil if no entry is found. See also get_entry

Searches for an entry just as find_entry, but throws Errno::ENOENT if no entry is found.

Returns an input stream to the specified entry. If a block is passed the stream object is passed to the block and the stream is automatically closed afterwards just as with ruby‘s builtin File.open method.

Returns an output stream to the specified entry. If a block is passed the stream object is passed to the block and the stream is automatically closed afterwards just as with ruby‘s builtin File.open method.

Returns a string containing the contents of the specified entry

Removes the specified entry.

Replaces the specified entry with the contents of srcPath (from the file system).

Returns the name of the zip archive

[Validate]