Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def zip_send
- filename = "#{Rails.root}/public/zip/archive.zip"
- temp_file = Tempfile.new(filename) #, 'w'
- input_filenames = Dir.entries("#{Rails.root}/public/zip/scripts/").select {|f| !File.directory? f}
- folder = "#{Rails.root}/public/zip/scripts/"
- begin
- # This is the tricky part - initialize the temp file as a zip file
- Zip::OutputStream.open(temp_file) { |zos| }
- # Add files to the zip file as usual
- Zip::File.open(temp_file.path, Zip::File::CREATE) do |zipfile|
- input_filenames.each do |file|
- zipfile.add("scripts/"+file, File.join(folder, file))
- end
- end
- # Read the binary data from the file
- zip_data = File.read(temp_file.path)
- send_data(zip_data, type: 'application/zip', filename: filename, disposition: 'attachment')
- ensure
- # Close and delete the temp file
- temp_file.close
- temp_file.unlink
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement