-
Notifications
You must be signed in to change notification settings - Fork 2
/
bzip2.go
35 lines (30 loc) · 887 Bytes
/
bzip2.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
package mutators
import (
"io"
"github.com/batmac/ccat/pkg/log"
"github.com/dsnet/compress/bzip2"
)
func init() {
singleRegister("bzip2", cbzip2, withDescription("compress to bzip2 data (X:9 is compression level, 0-9)"),
withCategory("compress"),
withConfigBuilder(stdConfigUint64WithDefault(9)),
)
// singleRegister("unbzip2alt", bunzip2Alt, withDescription("decompress bzip2 data (alt)"))
}
func cbzip2(w io.WriteCloser, r io.ReadCloser, config any) (int64, error) {
lvl := int(config.(uint64))
log.Debugf("compression level: %d", lvl)
zw, err := bzip2.NewWriter(w, &bzip2.WriterConfig{Level: lvl})
if err != nil {
log.Fatal(err)
}
defer zw.Close()
return io.Copy(zw, r) // streamable
}
/* func bunzip2Alt(w io.WriteCloser, r io.ReadCloser) (int64, error) {
bzr, err := bzip2.NewReader(r, nil)
if err != nil {
log.Fatal(err)
}
return io.Copy(w, bzr)
} */