Class: ZipFolder

Inherits:
Object
  • Object
show all
Defined in:
lib/zip_folder.rb,
lib/zip_folder/version.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 = ZipFolder.new(directory_to_zip, output_file)
zf.write()

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_dir, output_file) ⇒ ZipFolder

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



25
26
27
28
# File 'lib/zip_folder.rb', line 25

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

Class Method Details

.zip(input_dir, output_file) ⇒ Object



20
21
22
# File 'lib/zip_folder.rb', line 20

def self.zip(input_dir, output_file)
  new(input_dir, output_file).write
end

Instance Method Details

#writeObject

Zip the input directory.



31
32
33
34
35
36
37
# File 'lib/zip_folder.rb', line 31

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

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