Encode CSV Directly Into ZIP In flight
Published on February 7, 2023
Create chain of writers that will write CSV encoded data directly into archive file.
file, _ := os.CreateTemp("/var/tmp", "my_archive_*.zip")
// create zip archive writer
zipWriter := zip.NewWriter(file)
// create a file in the archive
file, _ := zipWriter.Create("my_csv_file.csv")
// create CSV writer and write data into file
csvWriter := csv.NewWriter(file)
_ = wr.Write([]string{"Value 1", "Value 2"})
// finalize work
csvWriter.Flush()
zipWriter.Flush()
zipWriter.Close()
file.Close()
Create writers chain
Create archive file
Create file on a filesystem. All zipped data will be written to this file. We use temporary file in this example:
zipFile, err := os.CreateTemp("/var/tmp", "my_csv_in_archive_*.zip")
if err != nil {
log.Fatal(err)
}
Create ZIP writer
Create new file in the archive with name my_data.csv
.
zipWriter := zip.NewWriter(zipFile)
dataFile, err := zipWriter.Create("my_data.csv")
if err != nil {
log.Fatal(err)
}
Write CSV data to ZIP file
Create CSV writer and write data into data file.
csvWriter := csv.NewWriter(dataFile)
for i := 0; i < 10; i++ {
err = csvWriter.Write([]string{"Column 1", "Column 2"})
if err != nil {
log.Fatal(err)
}
}
This code write CSV file with 10 rows. You can extract files from archive and check content of file.