简体   繁体   English

NameError-未初始化的常量Zip

[英]NameError - uninitialized constant Zip

I am trying to unzip a file in my Spree plugin. 我试图在Spree插件中解压缩文件。

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. 我已将rubyzip gem包含在我的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. 我已经尝试了stackoverflow和其他站点中提供的所有解决方案。 I tried downgrading the version of rubyzip which is 1.2.0 now and add require 'zip' or require 'zip/zip' . 我尝试降级rubyzip的版本(现在是1.2.0),并添加require'zip require 'zip'或require'zip require 'zip/zip' Both returned load error. 两者均返回加载错误。

I have try adding require 'zip/filesystem' to the class. 我尝试在类中添加require 'zip/filesystem' But got 但是得到了

LoadError - cannot load such file -- zip/zipfilesystem

Any solution for this? 有什么解决办法吗?

It's looking for a nested Constant. 它正在寻找嵌套的常量。 Change line Zip::File.open(zip) do |zip_file| 更改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 . 还要确保您需要rubygem / bundle setup Though in spree it should've already been done. 尽管应该大礼包,但它应该已经完成​​了。

Babar的答案是正确的,但是您还需要在application_controller.rb中添加require 'zip'

Include rubyzip in gem file in this way: 通过以下方式将rubyzip包含在gem文件中:

gem 'rubyzip', require: 'zip'

See this question 看到这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM