Evgeny Lazin
Evgeny Lazin

Reputation: 9423

Golang: can't decompress data with lzw package

I'm trying to create compressed string pool with Go. This is my code - http://play.golang.org/p/T5usLfU0fA

I can't decompress what have bin compressed with compress/lzw package. The input to the lzw.Writer is [104 101 108 108 111 32 119 111 114 108 100] and the output of the lzw.Reader is [0 1 0 0 3 0 3 3 2 0 0]. They definitely doesn't match.

I'm creating reader and writer with the same parameters (all except buffer). Buffer for lzw.Reader contains data, previously compressed with lzw.Writer.

Upvotes: 0

Views: 1250

Answers (1)

lnmx
lnmx

Reputation: 11164

Change your litWidth parameter for lzw.NewReader and lzw.NewWriter from 2 to 8.

I don't know much about LZW, but it looks like the litWidth determines how many bits from each incoming byte are considered significant. So, a value of 2 means that all input bytes must be 0x00 - 0x03.

See http://play.golang.org/p/svjeVFntuE for a minimal example of an LZW round-trip. A litWidth of 7 works (but is presumably not safe for UTF-8 text), anything less than 7 fails.

Upvotes: 5

Related Questions