I need to serve some data from my database in a zip file, streaming it on the fly such that:
- I do not write a temporary file to disk
- I do not compose the whole file in RAM
I know that I can do streaming generation of zip files to the filesystemk using ZipOutputStream as here. I also know that I can do streaming output from a rails controller by setting response_body to a Proc as here. What I need (I think) is a way of plugging those two things together. Can I make rails serve a response from a ZipOutputStream? Can I get ZipOutputStream give me incremental chunks of data that I can feed into my response_body Proc? Or is there another way?
I had a similar issue. I didn’t need to stream directly, but only had your first case of not wanting to write a temp file. You can easily modify ZipOutputStream to accept an IO object instead of just a filename.
From there, it should just be a matter of using the new Zip::IOOutputStream in your Proc. In your controller, you’d probably do something like: