Sha256: 3c8e74652c641956185da864de5814dffc4910cfc5d77a037ba05b68b9f8b3f1
Contents?: true
Size: 1.53 KB
Versions: 12
Compression:
Stored size: 1.53 KB
Contents
# encoding: utf-8 require 'zip' module Grache # This is a simple example which uses rubyzip to # recursively generate a zip file from the contents of # a specified directory. The directory itself is not # included in the archive, rather just its contents. # # Usage: # require /path/to/the/ZipFileGenerator/Class # directoryToZip = "/tmp/input" # outputFile = "/tmp/out.zip" # zf = ZipFileGenerator.new(directoryToZip, outputFile) # zf.write() class ZipGenerator # Initialize with the directory to zip and the location of the output archive. def initialize(inputDir, outputFile) @inputDir = inputDir @outputFile = outputFile end # Zip the input directory. def write() entries = Dir.entries(@inputDir); entries.delete('.'); entries.delete('..') io = Zip::File.open(@outputFile, Zip::File::CREATE); writeEntries(entries, '', io) io.close(); end # A helper method to make the recursion work. private def writeEntries(entries, path, io) entries.each { |e| zipFilePath = path == '' ? e : File.join(path, e) diskFilePath = File.join(@inputDir, zipFilePath) puts 'Deflating ' + diskFilePath 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.print(File.open(diskFilePath, 'rb').read()) } end } end end end
Version data entries
12 entries across 12 versions & 1 rubygems