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
+7k Golang : Squaring elements in array
+19.4k Golang : Get current URL example
+4.8k JQuery : Calling a function inside Jquery(document) block
+18.4k Golang : Find IP address from string
+8k Golang : Append and add item in slice
+14.3k Golang : How to check if your program is running in a terminal
+27.3k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+16.1k Golang : convert string or integer to big.Int type
+19.7k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+9k Golang : Intercept and compare HTTP response code example
+16.9k Golang : XML to JSON example