Sha256: fe14376bb81b76fda99f8a72d599114deabb562849325e3230524846c6806255

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

require 'zip'

module Stevenson
  module OutputFilters
    module ZipFilter
      def output(directory)
        Dir.mktmpdir do |dir|
          # Call the parent method
          super dir
  
          # Zip up the output directory
          write dir, directory
        end
      end

      private

      def write(inputDir, outputFile)
        @inputDir = inputDir
        @outputFile = outputFile

        entries = Dir.entries(@inputDir)
        entries.delete(".")
        entries.delete("..")
        io = Zip::File.open(@outputFile, Zip::File::CREATE)

        writeEntries(entries, "", io)
        io.close()
      end

      def writeEntries(entries, path, io)
        entries.each { |e|
          zipFilePath = path == "" ? e : File.join(path, e)
          diskFilePath = File.join(@inputDir, zipFilePath)
          if File.directory?(diskFilePath)
            io.mkdir(zipFilePath)
            subdir =Dir.entries(diskFilePath); subdir.delete("."); subdir.delete("..")
            writeEntries(subdir, zipFilePath, io)
          else
            io.get_output_stream(zipFilePath) { |f| f.puts(File.open(diskFilePath, "rb").read())}
          end
        }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stevenson-1.0.1 lib/stevenson/output_filters/zip.rb
stevenson-1.0.0 lib/stevenson/output_filters/zip.rb