DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

How To ZIP Complete Folder Without Path And With Name Of A Folder

01.08.2012
| 4850 views |
  • submit to reddit
        
require 'zip/zip'
require 'zip/zipfilesystem'
include Zip

def compress(ppath)
	ppath.sub!(%r[/$],'')
	archive = File.join('./',File.basename(ppath))+'.zip'
	FileUtils.rm archive, :force=>true
	Zip::ZipFile.open(archive, 'w') do |zipfile|
		Dir["#{ppath}/**/**"].reject{|f|f==archive}.each do |file|
			zipfile.add(file.sub(ppath+'/',''),file)
		end
	end
end

# usage
temp = "./some_folder"
begin 
	compress(temp)                  # pack folder
	FileUtils.remove_dir(temp,true) # remove non empty folder, which is packed
end