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_#{}_#{date}_#{timestamp}.zip")
headers =

csv = subscribers.export(, 'w'), columns: Customer::COLUMNS + headers) do |zos|
  zos.put_next_entry YOUR_CSV_FILE_NAME