-
Notifications
You must be signed in to change notification settings - Fork 294
/
compress.go
64 lines (56 loc) · 1.42 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
package compress
import (
"bytes"
"compress/gzip"
"fmt"
"io/ioutil"
"github.com/golang/snappy"
"github.com/xitongsys/parquet-go/parquet"
)
//Uncompress using Gzip
func UncompressGzip(buf []byte) ([]byte, error) {
rbuf := bytes.NewReader(buf)
gzipReader, _ := gzip.NewReader(rbuf)
res, err := ioutil.ReadAll(gzipReader)
return res, err
}
//Compress using Gzip
func CompressGzip(buf []byte) []byte {
var res bytes.Buffer
gzipWriter := gzip.NewWriter(&res)
gzipWriter.Write(buf)
gzipWriter.Close()
return res.Bytes()
}
//Uncompress using Snappy
func UncompressSnappy(buf []byte) ([]byte, error) {
return snappy.Decode(nil, buf)
}
//Compress using Snappy
func CompressSnappy(buf []byte) []byte {
return snappy.Encode(nil, buf)
}
func Uncompress(buf []byte, compressMethod parquet.CompressionCodec) ([]byte, error) {
switch compressMethod {
case parquet.CompressionCodec_GZIP:
return UncompressGzip(buf)
case parquet.CompressionCodec_SNAPPY:
return UncompressSnappy(buf)
case parquet.CompressionCodec_UNCOMPRESSED:
return buf, nil
default:
return nil, fmt.Errorf("Unsupported compress method")
}
}
func Compress(buf []byte, compressMethod parquet.CompressionCodec) []byte {
switch compressMethod {
case parquet.CompressionCodec_GZIP:
return CompressGzip(buf)
case parquet.CompressionCodec_SNAPPY:
return CompressSnappy(buf)
case parquet.CompressionCodec_UNCOMPRESSED:
return buf
default:
return nil
}
}