-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzlib_compress.go
More file actions
48 lines (40 loc) · 855 Bytes
/
zlib_compress.go
File metadata and controls
48 lines (40 loc) · 855 Bytes
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
package utils
import (
"bytes"
"compress/zlib"
"errors"
"io"
)
//数据以Zlib方式解压,参数为输入,返回为输出
func ZlibDecompress(data []byte) ([]byte, error) {
r, err := zlib.NewReader(String(data).NewReader())
if err != nil {
return nil, err
}
defer r.Close()
bs := NewByteString()
buff := make([]byte, 4096)
for {
if n, err := r.Read(buff); err != nil {
if err == io.EOF {
return bs.GetBuff(), nil
} else {
return bs.GetBuff(), err
}
} else {
bs.WriteBytes(buff, n)
}
}
}
//数据以zlib压缩,参数为输入,返回为输出
func ZlibCompress(data []byte) ([]byte, error) {
var b bytes.Buffer
w := zlib.NewWriter(&b)
if w == nil {
err := errors.New("zlib.NewWriter failed!")
return b.Bytes(), err
}
defer w.Close()
_, err := w.Write([]byte(data))
return b.Bytes(), err
}