Class: Iconly::ZipFileGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/iconly/zip_file_generator.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:

directory_to_zip = "/tmp/input"
output_file = "/tmp/out.zip"
zf = ZipFileGenerator.new(directory_to_zip, output_file)
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.



16
17
18
19
# File 'lib/iconly/zip_file_generator.rb', line 16

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

Instance Method Details

#writeObject

Zip the input directory.



22
23
24
25
26
27
28
# File 'lib/iconly/zip_file_generator.rb', line 22

def write
  entries = Dir.entries(@input_dir) - %w(. ..)

  ::Zip::File.open(@output_file, ::Zip::File::CREATE) do |io|
    write_entries entries, '', io
  end
end