diff --git a/util/util.go b/util/util.go index a41d077c..33897968 100644 --- a/util/util.go +++ b/util/util.go @@ -15,6 +15,8 @@ package util import ( + "bytes" + "compress/gzip" "context" "crypto/aes" "crypto/cipher" @@ -429,3 +431,23 @@ func UTF16EncodedByteArrayFromString(s string) ([]byte, error) { asBytes := Uint16ToByteArray(asUint16) return asBytes, nil } + +func CompressData(data []byte) ([]byte, error) { + var b bytes.Buffer + gz := gzip.NewWriter(&b) + + _, err := gz.Write(data) + if err != nil { + return nil, fmt.Errorf("failed to compress data: %w", err) + } + + if err = gz.Flush(); err != nil { + return nil, fmt.Errorf("failed to flush buffer: %w", err) + } + + if err = gz.Close(); err != nil { + return nil, fmt.Errorf("failed to close buffer: %w", err) + } + + return b.Bytes(), nil +}