Class: ZipFileGenerator

Inherits:
Object
  • Object
show all
Defined in:
app/models/zip_file_generator.rb

Overview

Bundles a directory tree into a single ZIP archive on disk.
Optionally deletes the source directory after writing
(cleanup: true) so callers can stage temporary export bundles
without manually unwinding them. Used by exports such as the
DesignToolFixture CSV pack and the catalog-item asset packet.

Instance Method Summary collapse

Constructor Details

#initialize(input_dir, output_file, cleanup: false) ⇒ ZipFileGenerator

Returns a new instance of ZipFileGenerator.

Parameters:

  • input_dir (String, Pathname)

    directory to compress

  • output_file (String, Pathname)

    destination zip path

  • cleanup (Boolean) (defaults to: false)

    whether to delete the source directory after writing



14
15
16
17
18
# File 'app/models/zip_file_generator.rb', line 14

def initialize(input_dir, output_file, cleanup: false)
  @input_dir = Pathname.new(input_dir)
  @output_file = output_file
  @cleanup = cleanup
end

Instance Method Details

#writevoid

This method returns an undefined value.

Zip the input directory and write to the output file.



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

def write
  entries = @input_dir.find.select(&:file?).map { |path| path.relative_path_from(@input_dir) }

  Zip::File.open(@output_file, create: true) do |zip|
    entries.each { |entry| add_entry_to_zip(entry, zip) }
  end

  cleanup_files if @cleanup
end