Sha256: 582365434176919940eda783653efb33df1bdd87edad4791afde65ca5c895248

Contents?: true

Size: 1.25 KB

Versions: 15

Compression:

Stored size: 1.25 KB

Contents

require 'zip'

module TerraformDevKit
  class ZipFileGenerator
    def initialize(input_dir, output_file)
      @input_dir = input_dir
      @output_file = output_file
    end

    def write
      entries = Dir.entries(@input_dir)
      entries.delete('.')
      entries.delete('..')
      Zip::File.open(@output_file, Zip::File::CREATE) do |zipfile|
        write_entries(entries, '', zipfile)
      end
    end

    private

    def write_entries(entries, path, zipfile)
      entries.each do |e|
        zip_file_path = path == '' ? e : File.join(path, e)
        disk_file_path = File.join(@input_dir, zip_file_path)
        if File.directory?(disk_file_path)
          write_directory(disk_file_path, zip_file_path, zipfile)
        else
          write_file(disk_file_path, zip_file_path, zipfile)
        end
      end
    end

    def write_directory(disk_file_path, zip_file_path, zipfile)
      zipfile.mkdir(zip_file_path)
      subdir = Dir.entries(disk_file_path)
      subdir.delete('.')
      subdir.delete('..')
      write_entries(subdir, zip_file_path, zipfile)
    end

    def write_file(disk_file_path, zip_file_path, zipfile)
      zipfile.get_output_stream(zip_file_path) do |f|
        f.puts(File.open(disk_file_path, 'rb').read)
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
TerraformDevKit-0.4.0 lib/TerraformDevKit/zip_file_generator.rb
TerraformDevKit-0.3.7 lib/TerraformDevKit/zip_file_generator.rb
TerraformDevKit-0.3.6 lib/TerraformDevKit/zip_file_generator.rb
TerraformDevKit-0.3.5 lib/TerraformDevKit/zip_file_generator.rb
TerraformDevKit-0.3.4 lib/TerraformDevKit/zip_file_generator.rb
TerraformDevKit-0.3.2 lib/TerraformDevKit/zip_file_generator.rb
TerraformDevKit-0.1.14 lib/TerraformDevKit/zip_file_generator.rb
TerraformDevKit-0.1.13 lib/TerraformDevKit/zip_file_generator.rb
TerraformDevKit-0.1.12 lib/TerraformDevKit/zip_file_generator.rb
TerraformDevKit-0.1.11 lib/TerraformDevKit/zip_file_generator.rb
TerraformDevKit-0.1.10 lib/TerraformDevKit/zip_file_generator.rb
TerraformDevKit-0.1.9 lib/TerraformDevKit/zip_file_generator.rb
TerraformDevKit-0.1.8 lib/TerraformDevKit/zip_file_generator.rb
TerraformDevKit-0.1.7 lib/TerraformDevKit/zip_file_generator.rb
TerraformDevKit-0.1.6 lib/TerraformDevKit/zip_file_generator.rb