Golang的zlib无法进行压缩。

huangapple go评论131阅读模式
英文:

Golang's zlib is not compressing

问题

我有以下一段代码,它将一个十六进制字符串转换为字节数组;但是当我尝试使用zlib压缩这个字节数组时,它不起作用(我在压缩前后打印了数据,但是它们是相同的):

data, err := hex.DecodeString(request.Log)
if err != nil {
    panic(err)
}
fmt.Println(data)
var writer bytes.Buffer
gz := zlib.NewWriter(&writer)
if _, err = gz.Write(data); err != nil {
    println("error: ", err)
    return
}

gz.Flush()
if err = gz.Close(); err != nil {
    println("err", err)
    return
}

fmt.Println(data)

我还尝试在关闭之前手动刷新写入器,但没有帮助。

英文:

I have the following piece of code that takes a hex string and converts it into byte array; But when I try to compress the mentioned byte array using zlib it doesn't work(I print the data before and after compression but there are the same):

		data, err := hex.DecodeString(request.Log)
		if err != nil {
			panic(err)
		}
		fmt.Println(data)
		var writer bytes.Buffer
		gz := zlib.NewWriter(&writer)
		if _, err = gz.Write(data); err != nil {
			println("error: ", err)
			return
		}

		gz.Flush()
		if err = gz.Close(); err != nil {
			println("err", err)
			return
		}

		fmt.Println(data)

I also tried to manually flush the writer before closing it but it didn't help.

答案1

得分: 2

似乎你在最后一行打印了错误的变量。应该是fmt.Println(writer.Bytes())

英文:

It seems that you are printing the wrong variable in last line. It should be fmt.Println(writer.Bytes())

huangapple
  • 本文由 发表于 2021年6月8日 16:04:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/67883733.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定