Golang program for implementation LZW Data Compression and Uncompression


LZW is a data compression method that takes advantage of this repetition. This compression method, (1) start with an initial model, (2) read data piece by piece, (3) and update the model and encode the data as you go along. LZW is a "dictionary"-based compression algorithm. LZW encodes data by referencing a dictionary. To encode a sub-string, only a single code number, corresponding to that sub-string's index in the dictionary, needs to be written to the output file.

Example

package main
import "fmt"
 

func compressLZW(testStr string) []int {    
    code := 256
    dictionary := make(map[string]int)
    for i := 0; i < 256; i++ {
        dictionary[string(i)] = i
    }
 
    currChar := ""
    result := make([]int, 0)	
    for _, c := range []byte(testStr) {	
        phrase := currChar + string(c)
        if _, isTrue := dictionary[phrase]; isTrue {		
            currChar = phrase
        } else {
            result = append(result, dictionary[currChar])
            dictionary[phrase] = code
            code++
            currChar = string(c)
        }
    }
    if currChar != "" {
        result = append(result, dictionary[currChar])
    }
    return result
}
 

func decompressLZW(compressed []int) string {    
    code := 256
    dictionary := make(map[int]string)
    for i := 0; i < 256; i++ {
        dictionary[i] = string(i)
    }
 
    currChar := string(compressed[0])
    result := currChar
    for _, element := range compressed[1:] {
        var word string
        if x, ok := dictionary[element]; ok {
            word = x
        } else if element == code {
            word = currChar + currChar[:1]
        } else {
            panic(fmt.Sprintf("Bad compressed element: %d", element))
        }
 
        result += word
        
        dictionary[code] = currChar + word[:1]
        code++
 
        currChar = word
    }
    return result
}
 
func main() {
	fmt.Print("Enter any string :")
	var testStr string
    fmt.Scanln(&testStr)
	
    compressed := compressLZW(testStr)
    fmt.Println("\nAfter Compression :", compressed)
	
    uncompression := decompressLZW(compressed)
    fmt.Println("\nAfter Uncompression :", uncompression)
}

Output

Enter any string :Australia
After Compression : [65 117 115 116 114 97 108 105 97]

After Uncompression : Australia

C:\golang\example>go run test.go
Enter any string :Germany

After Compression : [71 101 114 109 97 110 121]

After Uncompression : Germany
Reference links for algorithm:
https://www.cs.duke.edu/csed/curious/compression/lzw.html
Most Helpful This Week