diff --git a/g/encoding/gcompress/gcompress.go b/g/encoding/gcompress/gcompress.go index 57633a7eb..859f5b8cd 100644 --- a/g/encoding/gcompress/gcompress.go +++ b/g/encoding/gcompress/gcompress.go @@ -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() } \ No newline at end of file diff --git a/g/encoding/gxml/gxml.go b/g/encoding/gxml/gxml.go index b2e441057..f36639106 100644 --- a/g/encoding/gxml/gxml.go +++ b/g/encoding/gxml/gxml.go @@ -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 {