Ole Henrik Skogstrøm Ole Henrik Skogstrøm - 1 year ago 62
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)

Answer Source

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