3

I am trying to unzip a file in my Spree plugin.

Defined the unzipping method in a module which looks like this.

module ImportImages
  class Zipper
    def self.unzip(zip, unzip_dir, remove_after = false)
      Zip::File.open(zip) do |zip_file|
        zip_file.each do |f|
          f_path=File.join(unzip_dir, f.name)
          FileUtils.mkdir_p(File.dirname(f_path))
          zip_file.extract(f, f_path) unless File.exist?(f_path)
        end
      end
      FileUtils.rm(zip) if remove_after
    end
  end
end

I have included the rubyzip gem in my Gemfile.

gem 'rubyzip'
gem 'zip-zip'

When trying to run it, I am getting the following error.

NameError - uninitialized constant ImportImages::Zipper::Zip:

I have tried every solution provided in stackoverflow and other sites. I tried downgrading the version of rubyzip which is 1.2.0 now and add require 'zip' or require 'zip/zip'. Both returned load error.

I have try adding require 'zip/filesystem' to the class. But got

LoadError - cannot load such file -- zip/zipfilesystem

Any solution for this?

Aswathy
  • 654
  • 1
  • 12
  • 26

3 Answers3

6

Include rubyzip in gem file in this way:

gem 'rubyzip', require: 'zip'

See this question

Montells
  • 6,389
  • 4
  • 48
  • 53
4

It's looking for a nested Constant. Change line Zip::File.open(zip) do |zip_file| with below:

::Zip::File.open(zip) do |zip_file|

It should work. Also make sure you require rubygem/bundle setup. Though in spree it should've already been done.

Babar Al-Amin
  • 3,939
  • 1
  • 16
  • 20
3

Babar's answer is correct, but you also need to add require 'zip' in application_controller.rb

Patryk
  • 197
  • 1
  • 16