import (
"bytes"
"compress/zlib"
"fmt"
"io/ioutil"
)
/*
@Time : 2021/9/13 17:09
@Author: iokde.com
*/
func main() {
str := "iokde.com"
fmt.Println("origin info is :", str)
zipResult := zipStr(str)
fmt.Println("zip result :", zipResult)
origin := unzipStr(zipResult)
fmt.Printf("zip string decode result: %s", origin)
}
func zipStr(origin string) (content string) {
var b bytes.Buffer
w := zlib.NewWriter(&b)
w.Write([]byte(origin))
w.Close()
return b.String()
}
func unzipStr(zipContent string) (originInfo []byte) {
var b bytes.Buffer
b.WriteString(zipContent)
r, err := zlib.NewReader(&b)
if err != nil {
fmt.Println(" err : ", err)
}
defer r.Close()
//r.Close()
originInfo, err = ioutil.ReadAll(r)
if err != nil {
fmt.Println(" err : ", err)
}
return originInfo
}
标签:origin,zip,err,fmt,Println,字符串,go,string From: https://www.cnblogs.com/shif1537/p/17145486.html