Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kevit
Created April 21, 2015 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevit/f9ee97d525f1f49a9330 to your computer and use it in GitHub Desktop.
Save kevit/f9ee97d525f1f49a9330 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'rubygems'
require 'zip'
class ZipFileGenerator
# 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)
puts "Path " + zipFilePath
zipFilePath = zipFilePath.encode("UTF-8","Windows-1251")
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.puts(File.open(diskFilePath, "rb").read())}
end
}
end
end
directoryToZip=ARGV[0]
outputFile=directoryToZip+".zip"
puts outputFile
puts directoryToZip
zf = ZipFileGenerator.new(directoryToZip, outputFile)
zf.write()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment