ZIP files in Rails

Today, I needed to make a KMZ file from my KML in Rails. This was easy thanks to RubyZIP: gem install rubyzip Now for the Rails action: # In your controller private def zip(data, filename) zipfile = “/tmp/rubyzip-#{rand 32768}” Zip::ZipOutputStream::open(zipfile) do |io| io.put_next_entry(filename) io.write data end zippy = File.open(zipfile).read File.delete(zipfile) zippy end This makes a temp file, adds to it zip-style, reads it to a string, and deletes it. It leaves nothing behind on the file system. Then all you [...]