Golang gzipの圧縮と解凍
4161 ワード
1 package src
2
3 import (
4 "bytes"
5 "compress/gzip"
6 )
7
8 func GzipEncode(in []byte) ([]byte, error) {
9 var (
10 buffer bytes.Buffer
11 out []byte
12 err error
13 )
14 writer := gzip.NewWriter(&buffer)
15 _, err = writer.Write(in)
16 if err != nil {
17 writer.Close()
18 return out, err
19 }
20 err = writer.Close()
21 if err != nil {
22 return out, err
23 }
24
25 return buffer.Bytes(), nil
26 }
27
28 func GzipDecode(in []byte) ([]byte, error) {
29 reader, err := gzip.NewReader(bytes.NewReader(in))
30 if err != nil {
31 var out []byte
32 return out, err
33 }
34 defer reader.Close()
35
36 return ioutil.ReadAll(reader)
37 }