-
Notifications
You must be signed in to change notification settings - Fork 11
/
gzip.go
56 lines (45 loc) · 1 KB
/
gzip.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
package j8a
import (
"bytes"
"github.com/klauspost/compress/gzip"
"io/ioutil"
"sync"
)
const gzipLevel int = 1
var gzipMagicBytes = []byte{0x1f, 0x8b}
var gzipSmall = []byte{31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 170, 174, 5, 4, 0, 0, 255, 255, 67, 191, 166, 163, 2, 0, 0, 0}
var zipPool = sync.Pool{
New: func() interface{} {
var buf bytes.Buffer
w, _ := gzip.NewWriterLevel(&buf, gzipLevel)
return w
},
}
var unzipPool = sync.Pool{
New: func() interface{} {
buf := bytes.NewBuffer(gzipSmall)
r, _ := gzip.NewReader(buf)
return r
},
}
// Gzip a []byte
func Gzip(input []byte) *[]byte {
wrt, _ := zipPool.Get().(*gzip.Writer)
buf := &bytes.Buffer{}
wrt.Reset(buf)
_, _ = wrt.Write(input)
_ = wrt.Close()
defer zipPool.Put(wrt)
enc := buf.Bytes()
return &enc
}
// Gunzip a []byte
func Gunzip(input []byte) *[]byte {
rd, _ := unzipPool.Get().(*gzip.Reader)
buf := bytes.NewBuffer(input)
_ = rd.Reset(buf)
dec, _ := ioutil.ReadAll(rd)
_ = rd.Close()
defer unzipPool.Put(rd)
return &dec
}