Skip to content

Instantly share code, notes, and snippets.

@mikezter
Created November 16, 2009 12:06
Show Gist options
  • Save mikezter/235950 to your computer and use it in GitHub Desktop.
Save mikezter/235950 to your computer and use it in GitHub Desktop.
GZip Compression using Rails
after_filter :compress
def compress
if self.request.env['HTTP_ACCEPT_ENCODING'] and self.request.env['HTTP_ACCEPT_ENCODING'].match(/gzip/)
if self.response.headers["Content-Transfer-Encoding"] != 'binary'
begin
ostream = StringIO.new
gz = Zlib::GzipWriter.new(ostream)
gz.write(self.response.body)
self.response.body = ostream.string
self.response.headers['Content-Encoding'] = 'gzip'
ensure
gz.close
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment