Skip to content

Instantly share code, notes, and snippets.

@laginha87
Last active August 29, 2015 14:23
Show Gist options
  • Save laginha87/a32da4d74076483236ea to your computer and use it in GitHub Desktop.
Save laginha87/a32da4d74076483236ea to your computer and use it in GitHub Desktop.
zip paperclip attachment processor
require 'zip'
# requires gem rubyzip
# usage has_attached_file :attachment, path: path, processors: %i(archive), styles: {original: {format: 'zip', archived_file_name:}}
module Paperclip
class Archive < Processor
def make
tempfile = create_zip_tempfile
Zip::File.open(tempfile.path, Zip::File::CREATE) do |zip|
zip.add(archived_file_name, @file.path)
end
tempfile
end
def archived_file_name
return @file.original_filename unless options[:archived_file_name]
options[:archived_file_name] + File.extname(@file.original_filename)
end
def create_zip_tempfile
Tempfile.new(%w(archive .zip)).tap {|tempfile| Zip::OutputStream.open(tempfile) { } }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment