/
lzw.go
36 lines (29 loc) · 686 Bytes
/
lzw.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package compress
import (
"bytes"
"compress/lzw"
"github.com/xiaonanln/goworld/engine/gwioutil"
)
func NewLzwCompressor() Compressor {
fc := lzwCompressor{}
return fc
}
type lzwCompressor struct {
}
func (fc lzwCompressor) Compress(b []byte, c []byte) ([]byte, error) {
wb := bytes.NewBuffer(c)
lzwWriter := lzw.NewWriter(wb, lzw.LSB, 8)
n, err := lzwWriter.Write(b)
if err != nil {
return nil, err
}
if n != len(b) {
return nil, errNotFullyCompressed
}
lzwWriter.Close()
return wb.Bytes(), nil
}
func (fc lzwCompressor) Decompress(c []byte, b []byte) error {
lzwReader := lzw.NewReader(bytes.NewReader(c), lzw.LSB, 8)
return gwioutil.ReadAll(lzwReader, b)
}