Golang:无法使用lzw包解压数据。

huangapple go评论81阅读模式
英文:

Golang: can't decompress data with lzw package

问题

我正在尝试使用Go创建压缩的字符串池。这是我的代码 - http://play.golang.org/p/T5usLfU0fA

我无法解压使用compress/lzw包压缩的内容。lzw.Writer的输入是[104 101 108 108 111 32 119 111 114 108 100],lzw.Reader的输出是[0 1 0 0 3 0 3 3 2 0 0]。它们显然不匹配。

我使用相同的参数(除了缓冲区)创建读取器和写入器。lzw.Reader的缓冲区包含之前使用lzw.Writer压缩的数据。

英文:

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.

答案1

得分: 5

lzw.NewReaderlzw.NewWriterlitWidth参数从2更改为8。

我对LZW不太了解,但似乎litWidth确定了每个输入字节中有多少位被认为是有效的。因此,值为2意味着所有输入字节必须是0x00 - 0x03。

请参阅http://play.golang.org/p/svjeVFntuE,其中有一个LZW往返的最简示例。litWidth为7可以工作(但对于UTF-8文本可能不安全),小于7的值会失败。

英文:

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.

huangapple
  • 本文由 发表于 2014年4月22日 00:37:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/23201551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定