Sha256: de8c29819610e37c834d72a4da1a21470cc7fce1c21bf322f5639aae71a0add6

Contents?: true

Size: 1.59 KB

Versions: 47

Compression:

Stored size: 1.59 KB

Contents

require 'zip'


module Ciinabox
  module Util
    ###
    ### taken from https://github.com/rubyzip/rubyzip/tree/master/samples
    ###
    class ZipFileGenerator

      # Initialize with the directory to zip and the location of the output archive.
      def initialize(input_dir, output_file)
        @input_dir = input_dir
        @output_file = output_file
      end

      # Zip the input directory.
      def write
        entries = Dir.entries(@input_dir) - %w(. ..)

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

      private

      # A helper method to make the recursion work.
      def write_entries(entries, path, zipfile)
        entries.each do |e|
          zipfile_path = path == '' ? e : File.join(path, e)
          disk_file_path = File.join(@input_dir, zipfile_path)
          puts "Deflating #{disk_file_path}"

          if File.directory? disk_file_path
            recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
          else
            put_into_archive(disk_file_path, zipfile, zipfile_path)
          end
        end
      end

      def recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
        zipfile.mkdir zipfile_path
        subdir = Dir.entries(disk_file_path) - %w(. ..)
        write_entries subdir, zipfile_path, zipfile
      end

      def put_into_archive(disk_file_path, zipfile, zipfile_path)
        zipfile.get_output_stream(zipfile_path) do |f|
          f.write(File.open(disk_file_path, 'rb').read)
        end
      end
    end
  end
end

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
ciinabox-ecs-0.2.14 ext/zip_helper.rb
ciinabox-ecs-0.2.14.alpha.1545258054 ext/zip_helper.rb
ciinabox-ecs-0.2.13.alpha.1545253866 ext/zip_helper.rb
ciinabox-ecs-0.2.13 ext/zip_helper.rb
ciinabox-ecs-0.2.13.alpha.1538542925 ext/zip_helper.rb
ciinabox-ecs-0.2.13.alpha.1538542617 ext/zip_helper.rb
ciinabox-ecs-0.2.12.alpha.1531450459 ext/zip_helper.rb
ciinabox-ecs-0.2.12 ext/zip_helper.rb
ciinabox-ecs-0.2.12.alpha.1531204956 ext/zip_helper.rb
ciinabox-ecs-0.2.12.alpha.1531204953 ext/zip_helper.rb
ciinabox-ecs-0.2.12.alpha.1531192384 ext/zip_helper.rb
ciinabox-ecs-0.2.11 ext/zip_helper.rb
ciinabox-ecs-0.2.11.alpha.1531180538 ext/zip_helper.rb
ciinabox-ecs-0.2.11.alpha.1529998710 ext/zip_helper.rb
ciinabox-ecs-0.2.10.alpha.1529998182 ext/zip_helper.rb
ciinabox-ecs-0.2.10 ext/zip_helper.rb
ciinabox-ecs-0.2.10.alpha.1529554164 ext/zip_helper.rb
ciinabox-ecs-0.2.10.alpha.1529542046 ext/zip_helper.rb
ciinabox-ecs-0.2.10.alpha.1529494989 ext/zip_helper.rb
ciinabox-ecs-0.2.9 ext/zip_helper.rb