-
Notifications
You must be signed in to change notification settings - Fork 19
/
compression.go
74 lines (58 loc) · 1.77 KB
/
compression.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
72
73
74
package curator
import (
"bytes"
"compress/gzip"
"io/ioutil"
"github.com/bkaradzic/go-lz4"
)
var (
CompressionProviders = map[string]CompressionProvider{
"gzip": NewGzipCompressionProvider(),
"lz4": NewLZ4CompressionProvider(),
}
)
type CompressionProvider interface {
Compress(path string, data []byte) ([]byte, error)
Decompress(path string, compressedData []byte) ([]byte, error)
}
type GzipCompressionProvider struct {
level int
}
func NewGzipCompressionProvider() *GzipCompressionProvider {
return NewGzipCompressionProviderWithLevel(gzip.DefaultCompression)
}
func NewGzipCompressionProviderWithLevel(level int) *GzipCompressionProvider {
return &GzipCompressionProvider{level: level}
}
func (c *GzipCompressionProvider) Compress(path string, data []byte) ([]byte, error) {
var buf bytes.Buffer
if writer, err := gzip.NewWriterLevel(&buf, c.level); err != nil {
return nil, err
} else if _, err := writer.Write(data); err != nil {
return nil, err
} else if err := writer.Close(); err != nil {
return nil, err
} else {
return buf.Bytes(), nil
}
}
func (c *GzipCompressionProvider) Decompress(path string, compressedData []byte) ([]byte, error) {
buf := bytes.NewBuffer(compressedData)
if reader, err := gzip.NewReader(buf); err != nil {
return nil, err
} else if data, err := ioutil.ReadAll(reader); err != nil {
return nil, err
} else {
return data, err
}
}
type LZ4CompressionProvider struct{}
func NewLZ4CompressionProvider() *LZ4CompressionProvider {
return &LZ4CompressionProvider{}
}
func (c *LZ4CompressionProvider) Compress(path string, data []byte) ([]byte, error) {
return lz4.Encode(nil, data)
}
func (c *LZ4CompressionProvider) Decompress(path string, compressedData []byte) ([]byte, error) {
return lz4.Decode(nil, compressedData)
}