-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress.go
71 lines (56 loc) · 1.18 KB
/
compress.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package baticli
import (
"bytes"
"io/ioutil"
"github.com/klauspost/compress/flate"
)
func newCompressor(typ CompressorType) Compressor {
switch typ {
case CompressorType_Deflate:
return DeflateCompressor{}
default:
return NullCompressor{}
}
}
type Compressor interface {
Compress([]byte) ([]byte, error)
Uncompress([]byte) ([]byte, error)
String() string
}
type DeflateCompressor struct{}
func (c DeflateCompressor) Compress(i []byte) (o []byte, err error) {
var buf bytes.Buffer
w, err := flate.NewWriter(&buf, 6)
if err != nil {
return
}
defer w.Close()
_, err = w.Write(i)
if err != nil {
return
}
if err = w.Flush(); err != nil {
return
}
o = buf.Bytes()
return
}
func (c DeflateCompressor) Uncompress(i []byte) (o []byte, err error) {
r := flate.NewReader(bytes.NewReader(i))
defer r.Close()
o, _ = ioutil.ReadAll(r)
return
}
func (c DeflateCompressor) String() string {
return "deflate"
}
type NullCompressor struct{}
func (c NullCompressor) Compress(i []byte) (o []byte, err error) {
return i, nil
}
func (c NullCompressor) Uncompress(i []byte) (o []byte, err error) {
return i, nil
}
func (c NullCompressor) String() string {
return "null"
}