Module: Zip::FileSystem

Included in:
File
Defined in:
lib/zip/filesystem.rb,
lib/zip/filesystem/dir.rb,
lib/zip/filesystem/file.rb,
lib/zip/filesystem/file_stat.rb,
lib/zip/filesystem/directory_iterator.rb,
lib/zip/filesystem/zip_file_name_mapper.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’, create: true) do |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' }

end “‘

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’) do |zipfile|

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

end “‘

Defined Under Namespace

Classes: Dir, DirectoryIterator, File, ZipFileNameMapper

Instance Method Summary collapse

Instance Method Details

#dirObject

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



55
56
57
# File 'lib/zip/filesystem.rb', line 55

def dir
  @zip_fs_dir
end

#fileObject

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



62
63
64
# File 'lib/zip/filesystem.rb', line 62

def file
  @zip_fs_file
end

#initializeObject

:nodoc:



44
45
46
47
48
49
50
# File 'lib/zip/filesystem.rb', line 44

def initialize # :nodoc:
  mapped_zip       = ZipFileNameMapper.new(self)
  @zip_fs_dir      = Dir.new(mapped_zip)
  @zip_fs_file     = File.new(mapped_zip)
  @zip_fs_dir.file = @zip_fs_file
  @zip_fs_file.dir = @zip_fs_dir
end