Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 893
Nerian Avatar asked Feb 17 '11 14:02

Nerian


1 Answers

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
like image 183
Simone Carletti Avatar answered Oct 24 '22 15:10

Simone Carletti