0 Star 0 Fork 654

李明/gf

forked from John/gf 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
gcompress.go 1.32 KB
一键复制 编辑 原始数据 按行查看 历史
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
// 数据压缩/解压
package gcompress
import (
"bytes"
"compress/zlib"
"compress/gzip"
"io"
)
// 进行zlib压缩
func Zlib(data []byte) []byte {
if data == nil || len(data) < 13 {
return data
}
var in bytes.Buffer
w := zlib.NewWriter(&in)
w.Write(data)
w.Close()
return in.Bytes()
}
// 进行zlib解压缩
func UnZlib(data []byte) []byte {
if data == nil || len(data) < 13 {
return data
}
b := bytes.NewReader(data)
var out bytes.Buffer
r, err := zlib.NewReader(b)
if err != nil {
return nil
}
io.Copy(&out, r)
return out.Bytes()
}
//做gzip解压缩
func UnGzip(data []byte) []byte {
var buf bytes.Buffer
content := bytes.NewReader(data)
zipdata, err := gzip.NewReader(content)
if err != nil {
return nil
}
io.Copy(&buf, zipdata)
zipdata.Close()
return buf.Bytes()
}
//做gzip压缩
func Gzip(data []byte) []byte {
var buf bytes.Buffer
zip := gzip.NewWriter(&buf)
_, err := zip.Write(data)
if err != nil {
return nil
}
zip.Close()
return buf.Bytes()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/limingsky/gf.git
git@gitee.com:limingsky/gf.git
limingsky
gf
gf
c9a36a8224f1

搜索帮助