Golang compress/gzip.NewWriter() and NewWriterLevel() functions example
package compress/gzip
Golang compress/gzip.NewWriter() and NewWriterLevel() functions example
package main
import (
"compress/gzip"
"fmt"
"io"
"os"
)
func main() {
inputFile, err := os.Open("file.txt")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer inputFile.Close()
outputFile, err := os.Create("file.txt.gz")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer outputFile.Close()
// for default compression, use
// writer, err := gzip.NewWriter(outputFile)
writer, err := gzip.NewWriterLevel(outputFile, gzip.BestCompression)
if err != nil {
fmt.Println("NewWriterLevel error ", err)
os.Exit(1)
}
defer writer.Close()
io.Copy(writer, inputFile)
writer.Flush() // remember to flush the toilet!
}
References :
https://golang.org/pkg/compress/gzip/#NewWriter
https://golang.org/pkg/compress/gzip/#NewWriterLevel
https://www.socketloop.com/tutorials/golang-gzip-file-example
Advertisement
Something interesting
Tutorials
+37.5k Golang : Convert to io.ReadSeeker type
+8.8k Golang : Check a web page existence with HEAD request example
+6k Linux : How to fix Brother HL-1110 printing blank page problem
+14.9k Golang : Get IP addresses of a domain name
+10.3k Golang : Fuzzy string search or approximate string matching example
+14.5k Golang : Loop each day of the current month example
+4.3k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+3.7k Mac OSX : Get disk partitions' size, type and name
+13.4k Golang : Submit web forms without browser by http.PostForm example
+16.5k Golang : Aligning strings to right, left and center with fill example
+11.3k Golang : How to display image file or expose CSS, JS files from localhost?
+11.4k Swift : Convert (cast) Int or int32 value to CGFloat