Sha256: e8089b2bcfa8482db51dab5833d4973074f23c8edaf8d462cf0bea2ed134db38

Contents?: true

Size: 1.27 KB

Versions: 6

Compression:

Stored size: 1.27 KB

Contents

require 'zip'

module TerraformDevKit
  class ZipFileGenerator
    def initialize(input_dir, output_file)
      puts "REAL ONE"
      @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

6 entries across 6 versions & 1 rubygems

Version Path
TerraformDevKit-0.1.5 lib/TerraformDevKit/zip_file_generator.rb
TerraformDevKit-0.1.4 lib/TerraformDevKit/zip_file_generator.rb
TerraformDevKit-0.1.3 lib/TerraformDevKit/zip_file_generator.rb
TerraformDevKit-0.1.2 lib/TerraformDevKit/zip_file_generator.rb
TerraformDevKit-0.1.1 lib/TerraformDevKit/zip_file_generator.rb
TerraformDevKit-0.1.0 lib/TerraformDevKit/zip_file_generator.rb