Advertisement
Guest User

Untitled

a guest
Jul 26th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 0.94 KB | None | 0 0
  1.   def zip_send
  2.     filename = "#{Rails.root}/public/zip/archive.zip"
  3.     temp_file = Tempfile.new(filename) #, 'w'
  4.     input_filenames = Dir.entries("#{Rails.root}/public/zip/scripts/").select {|f| !File.directory? f}
  5.     folder = "#{Rails.root}/public/zip/scripts/"
  6.     begin
  7.       # This is the tricky part - initialize the temp file as a zip file
  8.       Zip::OutputStream.open(temp_file) { |zos| }
  9.       # Add files to the zip file as usual
  10.       Zip::File.open(temp_file.path, Zip::File::CREATE) do |zipfile|
  11.         input_filenames.each do |file|
  12.           zipfile.add("scripts/"+file, File.join(folder, file))
  13.         end
  14.       end
  15.  
  16.       # Read the binary data from the file
  17.       zip_data = File.read(temp_file.path)      
  18.       send_data(zip_data, type: 'application/zip', filename: filename, disposition: 'attachment')
  19.     ensure
  20.       # Close and delete the temp file
  21.       temp_file.close
  22.       temp_file.unlink
  23.     end
  24.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement