好吧我找到了下面的代碼來用Ruby解壓縮文件。如何用ruby解壓縮文件
def unzip_file (file, destination)
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)
}
}
end
在此之上,我使用以下內容來確保安裝所需的gems。
begin
require 'rubygems'
rescue LoadError
'gem install rubygems'
end
begin
require 'zip/zip'
rescue LoadError
'gem install rubyzip'
end
所以,當我打電話unzip_file我得到以下錯誤:
in `unzip_file': uninitialized constant Zip (NameError)
我在做什麼錯? 謝謝!
只要試一下':: Zip :: ZipFile.open(file_path)..' –
「gem install rubygems」然後就有光了。 – DiegoSalazar
如果一個Ruby腳本自己開始安裝gem,我會感到非常驚訝。最好簡單地放棄LoadError,然後大叫用戶自己安裝gem。 – Max