Skip to content

Commit

Permalink
Feat add zip #315 (#329)
Browse files Browse the repository at this point in the history
* add zipcompress
  • Loading branch information
raspberry-hu authored Oct 30, 2022
1 parent 401dc36 commit a44a652
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pkg/compressor/zip_compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,44 @@
*/

package compressor

import (
"bytes"
"io"

"github.com/klauspost/compress/zlib"
)

type Zip struct{}

func (z Zip) Compress(data []byte) ([]byte, error) {
var buf bytes.Buffer
var zp = zlib.NewWriter(&buf)
if _, err := zp.Write(data); err != nil {
return nil, err
}
if err := zp.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}

func (z Zip) Decompress(data []byte) ([]byte, error) {
var buf bytes.Buffer
buf.Write(data)
r, err := zlib.NewReader(&buf)
if err != nil {
return nil, err
}
if err := r.Close(); err != nil {
return nil, err
}
if _, err := io.Copy(&buf, r); err != nil {
return nil, err
}
return buf.Bytes(), nil
}

func (z Zip) GetCompressorType() CompressorType {
return CompressorZstd
}
23 changes: 23 additions & 0 deletions pkg/compressor/zip_compress_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package compressor

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestZipCompress(t *testing.T) {
str := "test"

g := &Zip{}

compressRes, err := g.Compress([]byte(str))
assert.NoError(t, err)
t.Logf("compress res: %v", string(compressRes))

decompressRes, err := g.Decompress(compressRes)
assert.NoError(t, err)
t.Logf("decompress res: %v", string(decompressRes))

assert.Equal(t, str, string(decompressRes))
}

0 comments on commit a44a652

Please sign in to comment.