[go gzip write]writes multiples files as a single gzip file #go #gzip #example
package main
func main() {
var writer io.Writer
gzipWriter := gzip.NewWriter(writer)
var files = []struct {
name string
comment string
modTime time.Time
data string
}{
{"file-1.txt", "file-header-1", time.Now(), "Hello Gophers - 1"},
{"file-2.txt", "file-header-2", time.Now(), "Hello Gophers - 2"},
}
for _, file := range files {
gzipWriter.Name = file.name
gzipWriter.Comment = file.comment
gzipWriter.ModTime = file.modTime
if _, err := gzipWriter.Write([]byte(file.data)); err != nil {
log.Fatal(err)
}
if err := gzipWriter.Close(); err != nil {
log.Fatal(err)
}
gzipWriter.Reset(writer)
}