增加gzip方法的封装,修改gxml能够支持GBK/GB18030自动转UTF-8

This commit is contained in:
wenzi1
2018-06-01 21:10:28 +08:00
parent 1c3e8cb9e9
commit 5bf65f1476
2 changed files with 33 additions and 4 deletions

View File

@ -10,6 +10,7 @@ package gcompress
import (
"bytes"
"compress/zlib"
"compress/gzip"
"io"
)
@ -38,4 +39,30 @@ func UnZlib(data []byte) []byte {
}
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()
}

View File

@ -18,6 +18,10 @@ import (
// 将XML内容解析为map变量
func Decode(xmlbyte []byte) (map[string]interface{}, error) {
//@author wenzi1
//@date 20180529
//XML中的encoding如果非UTF-8时mxj包中会自动转换但是自动转换时需要调用方提供转换的方法,这里提供一个空方法
//所以如果涉及到字符集转换那么需要用户自行转为utf8时再调用该方法
if strings.Index(string(xmlbyte), "encoding=\"UTF-8\"") == -1 {
charsetReader := func(charset string, input io.Reader) (io.Reader, error) {
reader := input
@ -47,10 +51,8 @@ func EncodeWithIndent(v map[string]interface{}, rootTag...string) ([]byte, error
// XML格式内容直接转换为JSON格式内容
func ToJson(xmlbyte []byte) ([]byte, error) {
//input, _ := ioutil.ReadAll(bytes.NewReader(xmlbyte))
//input:= bytes.NewReader(xmlbyte)
//charset := "UTF-8"
//@wenzi1 20180529
//@author wenzi1
//@date 20180529
//XML中的encoding如果非UTF-8时mxj包中会自动转换但是自动转换时需要调用方提供转换的方法,这里提供一个空方法
//所以如果涉及到字符集转换那么需要用户自行转为utf8时再调用该方法
if strings.Index(string(xmlbyte), "encoding=\"UTF-8\"") == -1 {