Over a million developers have joined DZone.

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

·

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

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}