2

On my system I have two different environment. First one is PHP-based and second is Rails5/Ruby2.4.0.

Let's say I have few files and when request made, I put those files in a ZIP file and then post to user. Files can be changed so, zip-before process is not an option.

I can use PHP library called ZipStream. This package allow me to directly zip the file and post to user browser without loading all files into memory and without creating temp files. I can easily handle GBs of files.

But the problem is, I cannot find anything for Rails/Ruby. All I can find is always do memory first then post to user. But i cannot load all files to memory.

Is there any way to achive this on Rails/Ruby just like on PHP does?

Anyone knows any library/project or a way to do it?

Dennis
  • 1,805
  • 3
  • 22
  • 41
  • Would you be able to use the zip command via the command line (run the cmd from ruby) and then attach that file? But I guess still the file is created. So Are you saying you don't want to have that zip file at all in the server? – coderhs Feb 26 '17 at 13:03
  • Exactly. I do not want that file in server. When browser made a request i build zip on-the-fly and send completed pieces to user. If I run zip command (which i can) i have to wait until zipping process is finish so this means a lot of time to wait for a file being before download actually starts. – Dennis Feb 26 '17 at 13:05
  • We can make the linux cmd to zip the contents to stdout, and we can limit the size of the files to be zipped. From reading the code of ZipStream its limiting the size of files to 20MB it seems. – coderhs Feb 26 '17 at 13:14
  • So we can zip the contents via cmd, store it to a ruby variable and then send it from rails in that file format. `zipdata = %x(zip -q - file)` ` – coderhs Feb 26 '17 at 13:15
  • This is a good starting. I will try now. Do you have alternate solution? For example; OneDrive/Yadisk or Dropbox does like this? I try them and instantly it start to download the zip file. Wonder how they do it. – Dennis Feb 26 '17 at 13:17
  • 2
    seen this? http://stackoverflow.com/questions/4797315/rails-on-the-fly-streaming-of-output-in-zip-format – Anthony Feb 26 '17 at 13:19
  • 1
    @Anthony, No I did not. I can say i search a lot of subjects but didn't comes up. Yes, this is exactly what I asked for. Also your link has gem link called Zipline which makes it more easy and -again- what I asked for. – Dennis Feb 26 '17 at 13:23
  • @Anthony could you please write this as an answer so I can mark it? – Dennis Feb 26 '17 at 13:26
  • 1
    I'll just mark your question as a duplicate, it will be easier for future readers. – Anthony Feb 26 '17 at 13:43

0 Answers0