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.



11
12
13
14
15
# File 'app/models/zip_file_generator.rb', line 11

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

Instance Method Details

#writeObject

Zip the input directory and write to the output file



18
19
20
21
22
23
24
25
26
# File 'app/models/zip_file_generator.rb', line 18

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