Stream CSV files to Zip


Sometimes you will need to create zip archive from big files, let’s say huge CSV for 10 Gigabytes.

In this case you can use rubyzip gem.

Using gem

::Zip::OutputStream is core thing here, it creates zip file as a stream, just pass filename and your file path.

zip_path = File.join(temp_dir, "export_#{user.id}_#{date}_#{timestamp}.zip")
headers = user.fields.map(&:name)

csv = subscribers.export(File.open(csv_path, 'w'), columns: Customer::COLUMNS + headers)
::Zip::OutputStream.open(zip_path) do |zos|
  zos.put_next_entry YOUR_CSV_FILE_NAME
  zos.puts File.read(csv_path)
end

Enjoy!