gpt4 book ai didi

ruby-on-rails - rubyzip - 无法加载此类文件 - zip

转载 作者:行者123 更新时间:2023-12-03 18:19:38 26 4
gpt4 key购买 nike

我正在尝试使用 ruby​​zip 在我的应用程序中动态创建 zip 文件。我正在使用 their readme作为起点。我将 gem 添加到我的 Gemfile 中:

gem 'rubyzip'

然后跑 bundle install并重新启动了 rails 服务器。

我的 Controller 代码是:
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'但它会产生相同的错误。

提前致谢!

最佳答案

您可能正在查看一个太新的示例。

如果您正在使用 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

关于ruby-on-rails - rubyzip - 无法加载此类文件 - zip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17956999/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com