-
Notifications
You must be signed in to change notification settings - Fork 28
/
zstd.go
44 lines (33 loc) · 832 Bytes
/
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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package dataprotect
import (
"fmt"
"github.com/klauspost/compress/zstd"
)
type ZStd struct {
}
func NewZStd() *ZStd {
return &ZStd{}
}
func (g *ZStd) Compress(input []byte) ([]byte, error) {
compressor, err := zstd.NewWriter(nil)
if err != nil {
return nil, err
}
return compressor.EncodeAll(input, nil), nil
}
func (g *ZStd) Decompress(input []byte) ([]byte, error) {
decompressor, err := zstd.NewReader(nil)
if err != nil {
return nil, fmt.Errorf("error creating zstd decompressor: %w", err)
}
defer decompressor.Close()
decompressedData, err := decompressor.DecodeAll(input, nil)
if err != nil {
return nil, fmt.Errorf("error reading decompressed data: %w", err)
}
return decompressedData, nil
}