Permalink
Browse files

Added a sample which shows how to use rubyzip to zip a directory recu…

…rsively.
  • Loading branch information...
1 parent 794c946 commit 7569b37d85e1f675c43319f358991e0e822d58f7 @kungfucraig committed Sep 21, 2011
Showing with 49 additions and 0 deletions.
  1. +49 −0 samples/example_recursive.rb
@@ -0,0 +1,49 @@
+require 'zip/zip'
+
+# 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:
+# directoryToZip = "/tmp/input"
+# outputFile = "/tmp/out.zip"
+# zf = ZipFileGenerator(directoryToZip, outputFile)
+# zf.write()
+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::ZipFile.open(@outputFile, Zip::ZipFile::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.puts(File.open(diskFilePath, "rb").read())}
+ end
+ }
+ end
+
+end
+

0 comments on commit 7569b37

Please sign in to comment.