【问题标题】:rubyZip Gem: Want to zip remote files in RoRrubyZip Gem:想要在 RoR 中压缩远程文件
【发布时间】:2012-06-23 11:50:49
【问题描述】:

我想在压缩后从我的网站下载照片。我正在使用 ruby​​Zip gem,但无法压缩远程文件。以下是场景:

我正在尝试从服务器压缩内容。内容是这样的

http://myApplication.s3.amazonaws.com/xxxxxxxx/image/image1.jpeg,

所以在“zipfile.add(attachment.document_file_name, attachment.document.url)”中,我分配了以下值:

document_file_name = image1.jpeg/image2.jpeg/image3.jpeg document.url = http://myApplication.s3.amazonaws.com/xxxxxxxx/image

现在我收到以下错误:

没有这样的文件或目录 - myApplication.s3.amazonaws.com/xxxxxxxx/image

如果我从本地文件系统(例如:/home/user/images)压缩文件,这个 gem 可以正常工作,但不能用于远程文件。

我做错了吗?有人可以帮我吗?或者任何其他可以做到这一点的宝石?

谢谢, -Tahniyat

【问题讨论】:

标签: ruby-on-rails-3 zipfile rubyzip


【解决方案1】:

您可以做的是首先从 s3 读取它,将其直接写入存档文件(将存档文件放入您的临时目录),提供它然后删除临时存档文件。这是一个小sn-p:

  require 'zip/zip'

  s3 = Aws::S3.new(S3_KEY, S3_SECRET)
  bucket_gen = Aws::S3Generator::Bucket.create(s3, S3_BUCKET)
  archive_file = "#{Rails.root}/tmp/archive.zip"

  Zip::ZipOutputStream.open(archive_file) do |zos|
    list_of_files_to_loop.each do |file|
      filename = file.filename
      url = "#{S3_PATH}/#{filename}"

      signed_url = bucket_gen.get(URI.unescape(URI.parse(URI.escape(url)).path[1..-1]), 1.minute)

      zos.put_next_entry(filename) # Give it next file a filename
      zos.print(URI.parse(signed_url).read) # Add file to zip
    end
  end # Write zip file

  # TODO: Serve file
  # TODO: Delete archived file from tmp directory

参考: http://rubyzip.sourceforge.net/classes/Zip/ZipOutputStream.html

【讨论】:

  • 新的 Zip 语法为:Zip::OutputStream.open(...)
  • 你只需要require 'zip',而不是require 'zip/zip'。见stackoverflow.com/questions/5997539/…
  • 如果这不起作用,请添加此 gem:'zip-zip'。为我工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-03
  • 1970-01-01
  • 1970-01-01
  • 2014-10-06
相关资源
最近更新 更多