Module: Zip::FileSystem

Included in:
File
Defined in:
lib/zip/filesystem.rb

Overview

The ZipFileSystem API provides an API for accessing entries in a zip archive that is similar to ruby’s builtin File and Dir classes.

Requiring ‘zip/filesystem’ includes this module in Zip::File making the methods in this module available on Zip::File objects.

Using this API the following example creates a new zip file my.zip containing a normal entry with the name first.txt, a directory entry named mydir and finally another normal entry named second.txt

require 'zip/filesystem'

Zip::File.open("my.zip", Zip::File::CREATE) {
  |zipfile|
  zipfile.file.open("first.txt", "w") { |f| f.puts "Hello world" }
  zipfile.dir.mkdir("mydir")
  zipfile.file.open("mydir/second.txt", "w") { |f| f.puts "Hello again" }
}

Reading is as easy as writing, as the following example shows. The example writes the contents of first.txt from zip archive my.zip to standard out.

require 'zip/filesystem'

Zip::File.open("my.zip") {
  |zipfile|
  puts zipfile.file.read("first.txt")
}

Defined Under Namespace

Classes: ZipFileNameMapper, ZipFsDir, ZipFsDirIterator, ZipFsFile

Instance Method Summary collapse

Instance Method Details

#dirObject

Returns a ZipFsDir which is much like ruby’s builtin Dir (class) object, except it works on the Zip::File on which this method is invoked



50
51
52
# File 'lib/zip/filesystem.rb', line 50

def dir
  @zipFsDir
end

#fileObject

Returns a ZipFsFile which is much like ruby’s builtin File (class) object, except it works on the Zip::File on which this method is invoked



57
58
59
# File 'lib/zip/filesystem.rb', line 57

def file
  @zipFsFile
end

#initializeObject

:nodoc:



39
40
41
42
43
44
45
# File 'lib/zip/filesystem.rb', line 39

def initialize # :nodoc:
  mappedZip = ZipFileNameMapper.new(self)
  @zipFsDir  = ZipFsDir.new(mappedZip)
  @zipFsFile = ZipFsFile.new(mappedZip)
  @zipFsDir.file = @zipFsFile
  @zipFsFile.dir = @zipFsDir
end