Add CompressData utility
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
parent
2fd170ec6f
commit
5a167186e2
1 changed files with 22 additions and 0 deletions
22
util/util.go
22
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue