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
+5k Python : Convert(cast) bytes to string example
+10.5k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+16.3k Golang : How to extract links from web page ?
+9.3k Golang : Temperatures conversion example
+8.7k Golang : Find duplicate files with filepath.Walk
+13.3k Golang : Linear algebra and matrix calculation example
+7.8k Swift : Convert (cast) String to Double
+5.2k Golang : Print instead of building pyramids
+8.2k Golang : Emulate NumPy way of creating matrix example
+19.4k Golang : How to count the number of repeated characters in a string?
+13.6k Golang : Query string with space symbol %20 in between
+18.5k Golang : Write file with io.WriteString