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
+11.5k Golang : Decompress zlib file example
+8.4k Golang : Find network service name from given port and protocol
+20.5k Golang : For loop continue,break and range
+10.3k Golang : Command line file upload program to server example
+8.4k Golang : Heap sort example
+5k Unix/Linux : How to archive and compress entire directory ?
+11.7k Golang : convert(cast) string to integer value
+25.8k Golang : Calculate future date with time.Add() function
+16.2k Golang : Gzip file example
+7.1k Golang : Gorrila set route name and get the current route name
+40.9k Golang : Convert string to array/slice
+5.8k Golang & Javascript : How to save cropped image to file on server