Monday 30 January 2012

Paperclip: Unzip/decompress uploaded file

Very often(in a client-server API architecture) we might want to transfer compressed file data over HTTP, so gain speed over file uploads.

So, this is how the decompression code looks like in Rails 3: For a Video model, with 'data' as the paperclip attachment and opts[:data] being the uploaded file.



    tmp_fle = Tempfile.new(opts[:data].original_filename.to_s)
    zi = Zlib::GzipReader.new(opts[:data])
    File.open(tmp_fle, "wb+") do |ucf|
      ucf << zi.read
    end
    zi.close
    tmp_fle.binmode
    self.data = tmp_fle

Hope it works.

No comments:

Post a Comment