forked from Roman2K/scat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gzip.go
44 lines (37 loc) · 747 Bytes
/
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
package procs
import (
"bytes"
"compress/gzip"
"io"
"io/ioutil"
"github.com/Roman2K/scat"
)
type Gzip struct {
// TODO level
}
func (gz Gzip) Proc() Proc {
return ChunkFunc(gz.process)
}
func (gz Gzip) Unproc() Proc {
return ChunkFunc(gz.unprocess)
}
func (Gzip) process(c *scat.Chunk) (new *scat.Chunk, err error) {
buf := &bytes.Buffer{}
w := gzip.NewWriter(buf)
_, err = io.Copy(w, c.Data().Reader())
if err != nil {
return
}
err = w.Close()
new = c.WithData(scat.BytesData(buf.Bytes()))
return
}
func (Gzip) unprocess(c *scat.Chunk) (new *scat.Chunk, err error) {
r, err := gzip.NewReader(c.Data().Reader())
if err != nil {
return
}
buf, err := ioutil.ReadAll(r)
new = c.WithData(scat.BytesData(buf))
return
}