Class: ZipFileGenerator

Inherits:
Object
  • Object
show all
Defined in:
app/services/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.



17
18
19
20
# File 'app/services/zip_file_generator.rb', line 17

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

Instance Method Details

#writeObject

Zip the input directory.



23
24
25
26
27
28
29
# File 'app/services/zip_file_generator.rb', line 23

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

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