vRp.CD2g_test/utils/compress/zip.go

48 lines
996 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package compress
import (
"archive/zip"
"bytes"
"io"
)
// ZIPCompress 对数据进行ZIP压缩返回bytes.Buffer和错误信息
func ZIPCompress(data []byte) (bytes.Buffer, error) {
var buf bytes.Buffer
zipWriter := zip.NewWriter(&buf)
f, err := zipWriter.Create("file")
if err != nil {
return buf, err
}
_, err = f.Write(data)
if err != nil {
return buf, err
}
if err := zipWriter.Close(); err != nil {
return buf, err
}
return buf, nil
}
// ZIPUnCompress 对已进行ZIP压缩的数据进行解压缩返回字节数组及错误信息
func ZIPUnCompress(dataByte []byte) ([]byte, error) {
data := bytes.NewReader(dataByte)
zipReader, err := zip.NewReader(data, int64(len(dataByte)))
if err != nil {
return nil, err
}
var result bytes.Buffer
for _, f := range zipReader.File {
rc, err := f.Open()
if err != nil {
return nil, err
}
_, err = io.Copy(&result, rc)
if err != nil {
return nil, err
}
rc.Close()
}
return result.Bytes(), nil
}