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
+6.8k Golang : Skip or discard items of non-interest when iterating example
+14.6k Golang : Rename directory
+9.5k Golang : Timeout example
+7.8k Golang : Test if an input is an Armstrong number example
+31k error: trying to remove "yum", which is protected
+10.8k Golang : Underscore string example
+6.1k Golang : Experimenting with the Rejang script
+5.9k Unix/Linux : Get reboot history or check when was the last reboot date
+21.7k Golang : How to read float value from standard input ?
+7.6k Golang : Detect sample rate, channels or latency with PortAudio
+12.3k Golang : convert(cast) string to integer value
+11k Golang : Sieve of Eratosthenes algorithm