ruby - Using rubzip2 and paperclip in rails, how to you create a temporary zip file and save it to paperclip? -


i had code preciously zipped load of files, created zip file in tmp file system , added files there.

i'm trying make more efficient using ruby's tempfile store file in code instead of manually writing tmp directory myself , deleting later on.

i've written code try , it…

def zip_up_files   require 'zip/zip'    t = tempfile.new(["temp-filename-#{export_type}", '.zip'], :type => 'application/zip')   zip::zipoutputstream.open(t.path) |z|     #pop files in zip...   end    #save has_attachment :download on model   self.download = file.open(t.path)   self.save    #delete tempfile...   t.close end 

this sucsfullt saved tempfile .download on model. if navigate saved zip file it's not zip file garbled text document. wondering if there's missing creation or saving of zip before get's saved modle. i've tried putting type tempfile creating. looks zip's missing mimetype. i'm not sure, can help?

thanks!

the problem is, you've written binary file disk, open in text mode rather binary mode. mess data.

# opens in text mode default! #self.download = file.open(t.path)  # change to... self.download = file.open(t.path, 'rb') 

incidentally, close not delete temp file. file getting deleted when file handle garbage collected. it's recommended call unlink explicitly delete temp file, otherwise you'll have tons of temp files on disk waiting garbage collected. (see synopsis , practices in tempfile docs.)

#delete tempfile... t.close t.unlink 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -