Class: ZipFileGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/dream-ops/utils/zip.rb

Overview

This is a simple example which uses rubyzip to recursively generate a zip file from the contents of a specified directory. The directory itself is not included in the archive, rather just its contents.

Usage:

directoryToZip = "/tmp/input"
outputFile = "/tmp/out.zip"
zf = ZipFileGenerator.new(directoryToZip, outputFile)
zf.write()

Instance Method Summary collapse

Constructor Details

#initialize(inputDir, outputFile) ⇒ ZipFileGenerator

Initialize with the directory to zip and the location of the output archive.



16
17
18
19
# File 'lib/dream-ops/utils/zip.rb', line 16

def initialize(inputDir, outputFile)
  @inputDir = inputDir
  @outputFile = outputFile
end

Instance Method Details

#writeObject

Zip the input directory.



22
23
24
25
26
27
28
# File 'lib/dream-ops/utils/zip.rb', line 22

def write()
  entries = Dir.entries(@inputDir); entries.delete("."); entries.delete("..")
  io = Zip::File.open(@outputFile, Zip::File::CREATE);

  writeEntries(entries, "", io)
  io.close();
end