Golang compress/lzw.NewWriter() function example
package compress/lzw
Golang compress/lzw.NewWriter() function usage example. For compressing data with LZW algorithm.
package main
import (
"compress/lzw"
"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.lzw")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer outputFile.Close()
// The number of bits to use for literal codes, litWidth, must be in the
// range [2,8] and is typically 8.
lzwWriter := lzw.NewWriter(outputFile, lzw.LSB, 8) // <---- here !
if err != nil {
fmt.Println("NewWriter error ", err)
os.Exit(1)
}
defer lzwWriter.Close()
io.Copy(lzwWriter, inputFile)
}
Reference :
Advertisement
Something interesting
Tutorials
+13.8k Golang : How to check for empty array string or string?
+23.7k Golang : Get current file path of a file or executable
+20.2k SSL : How to check if current certificate is sha1 or sha2
+17k Golang : Find IP address from string
+12.7k Golang : How to convert a number to words
+3.1k Detect if Google Analytics and Developer Media are loaded properly or not
+4.4k Golang : If else example and common mistake
+8.3k Golang : Find correlation coefficient example
+8.2k Golang : Read file with ioutil
+10.6k Golang : How to detect a server/machine network interface capabilities?
+4k Fix Google Analytics Redundant Hostnames problem
+9k Golang : Get escape characters \u form from unicode characters