From 5a167186e21f55ad8a6ad5f6741b6b173bdf99d2 Mon Sep 17 00:00:00 2001 From: Gabriel Adrian Samfira Date: Fri, 17 Mar 2023 13:17:53 +0200 Subject: [PATCH] Add CompressData utility Signed-off-by: Gabriel Adrian Samfira --- util/util.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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 +}