You will need to iterate over the items in the directory to add an entry to the compressed file.
def compress(path) gem 'rubyzip' require 'zip/zip' require 'zip/zipfilesystem' path.sub!(%r[/$],'') archive = File.join(path,File.basename(path))+'.zip' FileUtils.rm archive, :force=>true Zip::ZipFile.open(archive, 'w') do |zipfile| Dir["#{path}/**/**"].reject{|f|f==archive}.each do |file| zipfile.add(file.sub(path+'/',''),file) end end end
http://grosser.it/2009/02/04/compressing-a-folder-to-a-zip-archive-with-ruby/
Another way to do this is with the command
Dir["*"].each do |file| if File.directory?(file) #TODO add OS specific, # 7z or tar . `zip -r "#{file}.zip" "#{file}"` end end
http://ruby-indah-elegan.blogspot.com/2008/12/zipping-folders-in-folder-ruby-script.html
Update
Thanks Mahmoud Khaled for editing / updating
for the new version use Zip::File.open
instead of Zip::ZipFile.open
source share