2013-11-03 178 views

回答

20

這對我有效。使用子文件夾和文件解壓ziped文件夾時得到與您預期的相同的結果。從本網站

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

解決方案: http://bytes.com/topic/ruby/answers/862663-unzipping-file-upload-ruby

1

在Ruby中

提取Zip文件您需要rubyzip寶石這一點。一旦你已經安裝了它,你可以用這個方法來提取zip文件:

require 'zip' 

def extract_zip(file, destination) 
    FileUtils.mkdir_p(destination) 

    Zip::File.open(file) do |zip_file| 
    zip_file.each do |f| 
     fpath = File.join(destination, f.name) 
     zip_file.extract(f, fpath) unless File.exist?(fpath) 
    end 
    end 
end 

您可以使用它像這樣:

file_path = "/path/to/my/file.zip" 
destination = "/extract/destination/" 

extract_zip(file_path, destination) 
相關問題