Golang compress/flate.NewWriter() and NewWriterDict() functions example
package compress/flate
Golang compress/flate.NewWriter() and NewWriterDict() functions usage example.
package main
import (
"compress/flate"
"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.compressed")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer outputFile.Close()
flateWriter, err := flate.NewWriter(outputFile, flate.BestCompression)
if err != nil {
fmt.Println("NewWriter error ", err)
os.Exit(1)
}
defer flateWriter.Close()
io.Copy(flateWriter, inputFile)
flateWriter.Flush() // remember to flush!
}
and if you want to compress the file with your own dictionary, use NewWriterDict()
function instead
var dict = "test files for Golang"
flateWriter, err := flate.NewWriterDict(outputFile, flate.BestCompression, []byte(dict))
References :
Advertisement
Something interesting
Tutorials
+10.2k Golang : Simple file scaning and remove virus example
+24.5k Golang : Get executable name behind process ID example
+16k Golang : How to make a file read only and set it to writable again?
+8.2k Golang : How to get garbage collection data?
+7.8k Golang : Get curl -I or head data from URL example
+11.3k Golang : Covert map/slice/array to JSON or XML format
+3.7k JavaScript: Add marker function on Google Map
+5.8k Golang : Get expvar(export variables) to work with multiplexer
+13.9k Golang : Force download file example
+3.7k Chrome : How to block socketloop.com links in Google SERP?
+6.3k Golang : Word limiter example
+10.3k Golang : Secure file deletion with wipe example