encode as CSV then ZIP in golang
Asked Answered
M

1

6

How do I CSV encode and then zip?

Is there a way to chain a writer to another? Must I create a new buffer in between writers?

var buf bytes.Buffer

zipWriter := zip.NewWriter(&buf)
csvwriter := csv.NewWriter(zipWriter) // <--- zip writer doesn't implement `io.writer`

csvwriter.Write([]string{"a", "b\"fwr", "c"})
csvwriter.Write([]string{"a", "e", "ww"})
csvwriter.Flush()

println(buf.String())
Marble answered 19/12, 2019 at 22:55 Comment(1)
zip is an archive format, so have to add a file before you can write content. Use Create or CreateHeader.Gudrunguelderrose
Q
8

zip produces a structured zip file with a directory structure, which means you need to create and add each file independently.

An example playground demonstrates how to add your CSV file. It also demonstrates opening the compressed zip file and enumerating the files.


If you want to gzip the file as a single compressed stream, like:

$ gzip somefile.csv

to produce somefile.csv.gz

Then you can do the following as demostrated by this playground

package main

import (
    "bytes"
    "compress/gzip"
    "encoding/csv"
    "fmt"
    "io/ioutil"
)

func main() {
    var buf bytes.Buffer

    zipWriter := gzip.NewWriter(&buf)
    csvwriter := csv.NewWriter(zipWriter)

    csvwriter.Write([]string{"a", "b\"fwr", "c"})
    csvwriter.Write([]string{"a", "e", "ww"})
    csvwriter.Flush()
    zipWriter.Flush()
    zipWriter.Close()

    fmt.Println(buf.Bytes())
    zipReader, _ := gzip.NewReader(bytes.NewReader(buf.Bytes()))
    d, err := ioutil.ReadAll(zipReader)
    if err != nil {
        fmt.Println("err", err)
    }
    fmt.Println(string(d))
}
Quickly answered 20/12, 2019 at 0:38 Comment(1)
First example playground was what I needed. thanks!Marble

© 2022 - 2024 — McMap. All rights reserved.