Skip to content

Instantly share code, notes, and snippets.

@aquajach
Last active April 27, 2022 07:04
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aquajach/7fde54aa9bc1ac03740feb154e53eb7d to your computer and use it in GitHub Desktop.
Save aquajach/7fde54aa9bc1ac03740feb154e53eb7d to your computer and use it in GitHub Desktop.
Password protected zip file with csv/json data inside
compressed_filestream = Zip::OutputStream.write_buffer(::StringIO.new(''), Zip::TraditionalEncrypter.new('password')) do |zos|
#First file
zos.put_next_entry "test1.csv"
csv = CSV.generate do |csv|
User.all.each do |user|
csv << [user.id, user.name]
end
csv << values
end
zos.print csv
#Second file
zos.put_next_entry "test2.csv"
zos.print current_user.to_json
end
compressed_filestream.rewind
send_data compressed_filestream.read, filename: "test.zip"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment