Ruby zip stream

I am trying to write a ruby ​​fcgi script that compresses files in a directory on the fly and sends the output block by block as an HTTP response. It is very important that this compression is performed as a stream operation, otherwise the client will receive a timeout for huge directories.

I have the following code:

d="/tmp/delivery/" # send zip header header(MimeTypes::ZIP) # pseudocode from here on IO.open(d) { |fh| block=fh.readblock(1024) #send zipped block as http response print zip_it(block) } 

How to achieve what I wrote in a pseudo-ruby in the above list?

+7
source share
3 answers

The Tokland idea of ​​using an external zip command works very well. Here is a quick snippet that should work with Ruby 1.9 on Linux or a similar environment. It uses an array parameter for popen() to avoid any issues with shell quoting and sysread / syswrite to avoid buffering. You can display a status message in an empty rescue block if you want - or you could use read and write , although I have not tested them.

 #! usr/bin/env ruby d = '/tmp/delivery' output = $stdout IO.popen(['/usr/bin/zip', '-', d]) do |zip_output| begin while buf = zip_output.sysread(1024) output.syswrite(buf) end rescue EOFError end end 
+1
source

The AFAYK Zip format is not streaming; at the end of the compression, it writes something in the file header.

gz or tar.gz is the best option.

+1
source
0
source

All Articles