Ole Henrik Skogstrøm Ole Henrik Skogstrøm - 11 months ago 35
Ruby Question

How to unzip a zip file containing folders and files in rails, while keeping the directory structure

I need to extract a zip file that containes many folders and files using rails

gem. While also keeping the files and folders organized the way they where ziped.

This was not as straight forward as i though. Please see the solution i found beneath (added for future reference)


This worked for me. Gave the same result as you would expect when unziping a ziped folder with subfolders and files.

Zip::ZipFile.open(file_path) { |zip_file|
     zip_file.each { |f|
     f_path=File.join("destination_path", f.name)
     zip_file.extract(f, f_path) unless File.exist?(f_path)

Solution from this site: http://bytes.com/topic/ruby/answers/862663-unzipping-file-upload-ruby