Rails 3: How do I generate a compressed file on request
How to generate compressed files on request.
I have this controller
def create
send_data generate_tgz("#{RAILS_ROOT}/tmp/example.txt"), :filename => 'export.tgz'
end
But 开发者_开发百科it gives me a method not found on generate_tgz.
Is it a plugin or gem? Do I need to require anything? Can I generate a zip file instead?
Edit:
def generate_tgz(file)
system("tar -czf #{RAILS_ROOT}/tmp/export-result #{RAILS_ROOT}/tmp/export")
content = File.read("#{RAILS_ROOT}/tmp/export-result")
#ActiveSupport::Gzip.compress(content)
end
This creates a tgz, but when I decompress it I get app/c3ec2057-7d3a-40d9-9a9d-d5c3fe3ffd6f/home/tmp/export/and_the_files
I would like it to just be: export/the_files
The method doesn't exist. You can easily create it using ActiveSupport::Gzip.
def generate_tgz(file)
content = File.read(file)
ActiveSupport::Gzip.compress(content)
end
精彩评论