Class: ZipFileGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/helpers/zip_helpers.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(input_dir, output_file) ⇒ ZipFileGenerator

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



15
16
17
18
# File 'lib/helpers/zip_helpers.rb', line 15

def initialize(input_dir, output_file)
  @input_dir = input_dir
  @output_file = output_file
end

Instance Method Details

#writeObject

Zip the input directory.



21
22
23
24
25
26
27
28
29
# File 'lib/helpers/zip_helpers.rb', line 21

def write
  entries = Dir.entries(@input_dir)
  entries.delete('.')
  entries.delete('..')
  io = Zip::File.open(@output_file, Zip::File::CREATE)

  write_entries(entries, '', io)
  io.close
end