golang对字符串进行gzip压缩和Base64编码再由python3进行解码解压

2023-12-13 12:18:33

golang:

package main

import (
	"bytes"
	"compress/gzip"
	"encoding/base64"
	"fmt"
	"io"
)

func main() {
	var buf bytes.Buffer
	gz := gzip.NewWriter(&buf)
	defer gz.Close()
	var source_sql string
	source_sqls := "select * from table where id=1230 "
	// infos := "select * from table where id=?"
	id := 0
	var buf2 bytes.Buffer

	for {
		id++
		if id > 10 {
			break
		}
		// select * from table where id=10
		// 重置游标
		buf.Reset()
		gz = gzip.NewWriter(&buf)
		gz.Write([]byte(source_sqls))
		gz.Flush()
		gz.Close()
		source_sql = base64.StdEncoding.EncodeToString(buf.Bytes())
		fmt.Println("source_sql:", source_sql)
		source_sql2, err := base64.StdEncoding.DecodeString(source_sql)
		if err != nil {
			fmt.Println(err)
		}
		buf2.Reset()
		r, err := gzip.NewReader(bytes.NewReader(source_sql2))
		if err != nil {
			fmt.Println("err2:", err)
			return
		}
		io.Copy(&buf2, r)
		r.Close()
		fmt.Println(">>>>>", buf2.String())

	}

}

输出:

book@linda:/mnt/d/pyk$ go run main.go
source_sql: H4sIAAAAAAAA/ypOzUlNLlHQUkgrys9VKElMyklVKM9ILUpVyEyxNTQyNlAAAAAA//8BAAD//+28GngiAAAA
>>>>> select * from table where id=1230
source_sql: H4sIAAAAAAAA/ypOzUlNLlHQUkgrys9VKElMyklVKM9ILUpVyEyxNTQyNlAAAAAA//8BAAD//+28GngiAAAA
>>>>> select * from table where id=1230
source_sql: H4sIAAAAAAAA/ypOzUlNLlHQUkgrys9VKElMyklVKM9ILUpVyEyxNTQyNlAAAAAA//8BAAD//+28GngiAAAA
>>>>> select * from table where id=1230
source_sql: H4sIAAAAAAAA/ypOzUlNLlHQUkgrys9VKElMyklVKM9ILUpVyEyxNTQyNlAAAAAA//8BAAD//+28GngiAAAA
>>>>> select * from table where id=1230
source_sql: H4sIAAAAAAAA/ypOzUlNLlHQUkgrys9VKElMyklVKM9ILUpVyEyxNTQyNlAAAAAA//8BAAD//+28GngiAAAA
>>>>> select * from table where id=1230
source_sql: H4sIAAAAAAAA/ypOzUlNLlHQUkgrys9VKElMyklVKM9ILUpVyEyxNTQyNlAAAAAA//8BAAD//+28GngiAAAA
>>>>> select * from table where id=1230
source_sql: H4sIAAAAAAAA/ypOzUlNLlHQUkgrys9VKElMyklVKM9ILUpVyEyxNTQyNlAAAAAA//8BAAD//+28GngiAAAA
>>>>> select * from table where id=1230
source_sql: H4sIAAAAAAAA/ypOzUlNLlHQUkgrys9VKElMyklVKM9ILUpVyEyxNTQyNlAAAAAA//8BAAD//+28GngiAAAA
>>>>> select * from table where id=1230
source_sql: H4sIAAAAAAAA/ypOzUlNLlHQUkgrys9VKElMyklVKM9ILUpVyEyxNTQyNlAAAAAA//8BAAD//+28GngiAAAA
>>>>> select * from table where id=1230
source_sql: H4sIAAAAAAAA/ypOzUlNLlHQUkgrys9VKElMyklVKM9ILUpVyEyxNTQyNlAAAAAA//8BAAD//+28GngiAAAA
>>>>> select * from table where id=1230

python3:

#!/bin/python3

import base64
import gzip

str1 = "H4sIAAAAAAAA/ypOzUlNLlHQUkgrys9VKElMyklVKM9ILUpVyEyxNTQyNlAAAAAA//8BAAD//+28GngiAAAA" #""
print(str1)
try:
    b64sqlInfo = base64.b64decode(str1)
    # print(b64sqlInfo)
    strSql = gzip.decompress(b64sqlInfo).decode("utf8")
    print(strSql)
except Exception as e:
    print(e)

输出:

book@linda:/mnt/d/pyk$ python3 a.py
H4sIAAAAAAAA/ypOzUlNLlHQUkgrys9VKElMyklVKM9ILUpVyEyxNTQyNlAAAAAA//8BAAD//+28GngiAAAA
select * from table where id=1230

文章来源:https://blog.csdn.net/zhangh571354026/article/details/134873349
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。