awesome-go/impl/files-manipulation/25-CompressFile.go

31 lines
427 B
Go

package main
import (
"compress/gzip"
"log"
"os"
)
func main() {
/*
Compress a File
*/
// Create .gz file to write to
outputFile, err := os.Create("test.txt.gz")
if err != nil {
log.Fatal(err)
}
gzipWriter := gzip.NewWriter(outputFile)
defer gzipWriter.Close()
_, err = gzipWriter.Write([]byte("Gophers rule!\n"))
if err != nil {
log.Fatal(err)
}
log.Println("Compressed data written to file.")
}