Saturday, February 09, 2008

Rails Send Data Through a Pipe with IO.popen

While having trouble getting Unix zip to play nice with Ruby's Tempfile I found that it was much easier to just avoid a temp file altogether by using a pipe create with IO.popen. Example code below shows how you might zip up a directory and send it back to the user from a controller:

def self.make_zip_io
cmd = "sh -c 'cd /somewhere && zip -r - stuff'"
IO.popen(cmd)
end
def zip
io = self.class.make_zip_io
send_data(io.read, :filename => 'filename.zip', :type => 'application/zip')
end
view raw gistfile1.rb hosted with ❤ by GitHub