rubyZip Gem: Want to zip remote files in RoR
Asked Answered
M

1

5

I want to download photos from my website after zipping. I am using rubyZip gem but unable to zip remote files. Following is the scenario:

I am trying to zip content from server. Content is something like this

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

So in "zipfile.add( attachment.document_file_name, attachment.document.url)", i assigned following values:

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

Now here I am getting following error:

No such file or directory - myApplication.s3.amazonaws.com/xxxxxxxx/image

This gem is working fine if I zipped files from local file system (e.g: /home/user/images) but not for remote files.

Is I am doing something wrong? Can someone help me out? Or any other gem which can do this?

Thanks, -Tahniyat

Melano answered 23/6, 2012 at 11:50 Comment(1)
See this library: github.com/fringd/ziplineDenudate
D
9

What you can do is read it first from s3 write it directly to the archive file (put archive file to your temporary directory), serve it then delete the temp archive file. Here's a little snippet:

  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

Reference: http://rubyzip.sourceforge.net/classes/Zip/ZipOutputStream.html

Dailey answered 8/4, 2013 at 8:31 Comment(3)
New Zip syntax for this would be: Zip::OutputStream.open(...)Instrumentalist
And you only need to require 'zip', instead of require 'zip/zip'. See #5998039Tithe
If that doesn't work, add this gem: 'zip-zip'. Works for me.Elisavetpol

© 2022 - 2024 — McMap. All rights reserved.