我正在尝试使用rubyzip在我的应用程序中动态创建zip归档。我使用their readme作为起点。我将gem添加到我的Gemfile中:
gem 'rubyzip'然后运行bundle install并重启rails服务器。
我的控制器代码是:
require 'rubygems'
require 'zip'
filename = "#{Time.now.strftime('%y%m%d%H%M%S')}"
input_filenames = ["#{filename}.txt"]
zip_filename = "#{filename}.zip"
Zip::File.open(zip_filename, Zip::File::CREATE) do |zipfile|
input_filenames.each do |f|
zipfile.add(f, directory + '/' + f)
end
end我得到的错误是:cannot load such file -- zip
我尝试过require 'zip/zip',但它产生了相同的错误。
提前感谢!
发布于 2013-07-31 19:20:24
您可能正在查看一个太新的示例。
如果您使用的是Rubyzip 0.9.x,则需要遵循this example
(require "zip/zip",然后使用Zip::ZipFile而不是Zip::File)
require 'zip/zip'
folder = "Users/me/Desktop/stuff_to_zip"
input_filenames = ['image.jpg', 'description.txt', 'stats.csv']
zipfile_name = "/Users/me/Desktop/archive.zip"
Zip::ZipFile.open(zipfile_name, Zip::ZipFile::CREATE) do |zipfile|
input_filenames.each do |filename|
# Two arguments:
# - The name of the file as it will appear in the archive
# - The original file, including the path to find it
zipfile.add(filename, folder + '/' + filename)
end
end发布于 2013-12-18 22:42:45
我建议你使用:
gem 'rubyzip', "~> 1.1", require: 'zip'然后,控制器中的require 'zip'应该可以正常工作。
发布于 2013-08-29 15:56:30
更改了rubyzip的界面。您使用的是旧版本。主分支的rubydoc.info上的文档。
https://stackoverflow.com/questions/17956999
复制相似问题