ruby unzip - 因未初始化常量 ZIP::File 而失败

问题描述 投票:0回答:2

我有以下红宝石代码:

require 'HTTPClient'
require 'rubygems'
require 'zip'

def self.unzip(data, dest_dir)
  ::Zip::File.open_buffer(data) do |fzip|
    fzip.each do |entry|
     path = File.join(dest_dir, entry.name)
     puts "here"
     FileUtils::mkdir_p(File.dirname(path))
     fzip.extract(entry, path) unless File.exist?(path)
   #fzip.close
    end
  end
end

def self.fetch_from_url(url, dest_dir)
  response = HTTPClient.get(url, follows_redirect: true)
  if response.status == 200
    unzip(response.body, dest_dir)
  else
    raise 'Could not fetch files from 3scale'
  end
end


url = 'link/artifactory/zip-release-local/djin/3Sroxy/1.0.5/configuration.zip'
fetch_from_url(url, "/Users/something/")

当我在 Mac ruby 2.0.0p481 中运行它时,它工作正常并解压缩文件夹,但是当我在 ruby 1.8.7 中的 centOS 6.6 中运行相同的命令时,它会失败并显示以下内容:

[root@ip-10-201-90-206 ~]# sudo ruby test/ex.rb 
test/ex.rb:7:in `unzip': uninitialized constant Zip::File (NameError)
    from test/ex.rb:20:in `fetch_from_url'
    from test/ex.rb:28

另外,我已经完成了:

gem install zip  
Successfully installed zip-2.0.2
1 gem installed
Installing ri documentation for zip-2.0.2...
Installing RDoc documentation for zip-2.0.2... 
ruby-on-rails ruby ruby-on-rails-3 rubygems chef-infra
2个回答
9
投票

正如@Prashant4224所述,您需要:

gem uninstall zip
gem install rubyzip

2
投票

在 Gemfile 中需要添加。其他一切都不适合我。

gem 'rubyzip', '>= 1.0.0'
gem 'zip-zip'
© www.soinside.com 2019 - 2024. All rights reserved.