Class: Zippy
- Inherits:
-
Object
- Object
- Zippy
- Defined in:
- lib/albacore/tools/zippy.rb
Overview
github.com/aussiegeek/rubyzip/blob/master/samples/example_recursive.rb 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: dir_to_zip = “/tmp/input” out_file = “/tmp/out.zip” zf = Zippy.new dir_to_zip, out_file zf.write
Or: z = Zippy.new(directory_to_zip, output_file) { |f| f.include? ‘html’ } z.write
Instance Method Summary collapse
-
#initialize(input_dir, out_file, &filter) ⇒ Zippy
constructor
Initialize with the directory to zip and the location of the output archive.
-
#write ⇒ Object
Zips the input directory.
Constructor Details
#initialize(input_dir, out_file, &filter) ⇒ Zippy
Initialize with the directory to zip and the location of the output archive.
30 31 32 33 34 35 36 |
# File 'lib/albacore/tools/zippy.rb', line 30 def initialize input_dir, out_file, &filter @input_dir = input_dir. gsub(/[\/\\]$/, ''). gsub(/\\/, '/') @out_file = out_file @filter = block_given? ? filter : lambda { |f| true } end |
Instance Method Details
#write ⇒ Object
Zips the input directory.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/albacore/tools/zippy.rb', line 39 def write FileUtils.rm @out_file if File.exists? @out_file in_progress "Writing archive #{@out_file} from #{@input_dir}" do Zip::File.open @out_file, Zip::File::CREATE do |zipfile| Dir["#{@input_dir}/**/**"].reject{ |f| f == @out_file || !@filter.call(f) }.each do |file| progress "deflating #{file}" zipfile.add(file.sub(@input_dir + '/', ''), file) end end end end |