-
Notifications
You must be signed in to change notification settings - Fork 2
/
zstd.go
51 lines (42 loc) · 1.22 KB
/
zstd.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
package mutators
import (
"archive/zip"
"io"
"github.com/batmac/ccat/pkg/log"
"github.com/klauspost/compress/zstd"
)
func init() {
registerAsZipDecompressor()
singleRegister("unzstd", unzstd, withDescription("decompress zstd data"),
withCategory("decompress"),
)
singleRegister("zstd", czstd, withDescription("compress to zstd data (X:4 is compression level, 1-22)"),
withCategory("compress"),
withConfigBuilder(stdConfigUint64WithDefault(uint64(zstd.SpeedDefault))),
)
}
func registerAsZipDecompressor() {
decomp := zstd.ZipDecompressor()
zip.RegisterDecompressor(zstd.ZipMethodWinZip, decomp)
zip.RegisterDecompressor(zstd.ZipMethodPKWare, decomp)
}
func unzstd(out io.WriteCloser, in io.ReadCloser, _ any) (int64, error) {
d, err := zstd.NewReader(in)
if err != nil {
return 0, err
}
defer d.Close()
n, err := io.Copy(out, d)
return n, err
}
func czstd(out io.WriteCloser, in io.ReadCloser, conf any) (int64, error) {
encoderLvl := zstd.EncoderLevelFromZstd(int(conf.(uint64)))
log.Debugf("zstd compression level: %d (-> %v)\n", conf.(uint64), encoderLvl)
e, err := zstd.NewWriter(out, zstd.WithEncoderLevel(encoderLvl))
if err != nil {
return 0, err
}
n, err := io.Copy(e, in)
e.Close()
return n, err
}