class Grache::ZipGenerator
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()
Public Class Methods
new(inputDir, outputFile)
click to toggle source
Initialize with the directory to zip and the location of the output archive.
# File lib/grache/zip/zip_generator.rb, line 20 def initialize(inputDir, outputFile) @inputDir = inputDir @outputFile = outputFile end
Public Instance Methods
write(root = '')
click to toggle source
Zip the input directory.
# File lib/grache/zip/zip_generator.rb, line 26 def write(root = '') entries = Dir.entries(@inputDir) entries.delete('.') entries.delete('..') io = Zip::File.open(@outputFile, Zip::File::CREATE); writeEntries(entries, '', io, root) io.close(); end
Private Instance Methods
writeEntries(entries, path, io, root = '')
click to toggle source
A helper method to make the recursion work.
# File lib/grache/zip/zip_generator.rb, line 39 def writeEntries(entries, path, io, root = '') 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 iop = root.empty? ? zipFilePath : File.join(root, zipFilePath) # puts "IOP: #{iop}" io.get_output_stream(iop) do |f| f.print(File.open(diskFilePath, 'rb').read()) end end } end