Paperclip makes it easy to upload and download an attached files
individually. I've been struggling with how to download all of the
attachments with into a single compressed file. I'll take .zip, .gz,
whatever works is fine.
On Rails 2.1.2, responding to .zip format in my controller I have the
following code. Inside the Zip::ZipFile.open block, I have a number of
methods that I've tried. Each has listed below the unsuccessful result
with the resulting rails error if applicable.
Perhaps, using the rubyzip gem is what's causing the failure. I have
not tried using zlib. Another alternative would be to just compress
the /abstract folder and send that instead of going through
paperclip.
Any suggestions?
Thanks,
Chase
format.zip {
registrations_with_attachments = Registration.find_by_sql
('SELECT * FROM registrations WHERE abstract_file_name NOT LIKE ""')
headers['Cache-Control'] = 'no-cache'
tmp_filename = "#{RAILS_ROOT}/tmp/tmp_zip_" <<
Time.now.to_f.to_s <<
".zip"
# rubyzip gem version 0.9.1
# rdoc
http://rubyzip.sourceforge.net/
Zip::ZipFile.open(tmp_filename, Zip::ZipFile::CREATE) do |
zip|
#registrations_with_attachments.each { |e| zip.read
(e.abstract.url) }
#=> Error: Errno::ENOENT in
RegistrationsController#index. No such file or directory - /abstracts/
cN2_iGSSGr3yw0abTJTXpe/original/test.docx?1226721077
#zip.get_input_stream
(registrations_with_attachments.each { |e| send_data
(e.abstract.url) })
#=> Result: Apparently Corrupt .zip file with the wrong
filename (registrations.zip)
#registrations_with_attachments.each { |e| zip.add
("abstracts/#{e.abstract.original_filename}", send_data
(e.abstract.url)) }
#=> Result: Apparently Corrupt .zip file with the wrong
file name. Filename should be something like
tmp_zip_1226936644.54502.zip; Sent as registrations.zip; Mac OS X
Archive Utililty compresses into registrations.zip.cpgz
#registrations_with_attachments.each { |e| zip.add
("abstracts/#{e.abstract.original_filename}", send_file
(e.abstract.url)) }
#=> Error: ActionController::MissingFile in
RegistrationsController#index. Cannot read file /abstracts/
cN2_iGSSGr3yw0abTJTXpe/original/test.docx?1226721077
#registrations_with_attachments.each { |e| zip.add
("abstracts/#{e.abstract.original_filename}", e.abstract.url) }
#=> Error: Errno::ENOENT in
RegistrationsController#index. No such file or directory - /abstracts/
cN2_iGSSGr3yw0abTJTXpe/original/test.docx?1226721077
end
# send data through the browser
# sending with just the contents of the empty tmp_file
also results in an apparently corrupt zip file, but sent with the
filename as the entire pwd-tmp_zip_1226936896.53403.zip
send_data(File.open(tmp_filename, "rb+").read, :type =>
'application/zip', :disposition => 'attachment', :filename =>
tmp_filename.to_s)
File.delete tmp_filename
}