Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby unzip - fails with uninitialized constant ZIP::File

I have the following ruby code :

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/")

When I run this in Mac ruby 2.0.0p481 it works fine and unzips the folder, but when I run the same in centOS 6.6 in ruby 1.8.7 it fails with following :

[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

also, I've done :

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... 
like image 328
Scooby Avatar asked Jan 07 '23 14:01

Scooby


2 Answers

As Prashant4224 stated, you need to install

gem install rubyzip

You seem to use the zip Gem...

like image 62
tobmatth Avatar answered Jan 29 '23 04:01

tobmatth


In Gemfile you need to add. Everything else didn't work for me.

gem 'rubyzip', '>= 1.0.0'
gem 'zip-zip'
like image 40
Misha Avatar answered Jan 29 '23 04:01

Misha