Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rubyZip Gem: Want to zip remote files in RoR

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

like image 413
tahniyat Avatar asked Jun 23 '12 11:06

tahniyat


People also ask

How do I zip a file in R?

To zip files in R, use the zip() function. The zipped file is stored inside the current directory unless a different path is specified in the zip() function argument. The zip() method creates a new ZIP archive, and it overwrites the output file if it exists.

What is rubyzip?

Rubyzip is a ruby library for reading and writing zip files.


1 Answers

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

like image 72
index Avatar answered Oct 01 '22 23:10

index