How can I use zlib in golang to cooperate with zlib in c?

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

How can I use zlib in golang to cooperate with zlib in c?

问题

我发现,在使用golang中的zlib时,对于相同的字符串,结果与c中的不同。我该如何在golang中使用zlib进行压缩,并在c中进行解压缩?go语言使用哪个版本的zlib?

英文:

I found that, for the same string, the result of using zlib in golang is different with that in c. How can I compress in golang and decompress in c by zlib ? Which version does go use?

答案1

得分: 4

尽管压缩数据不同,并不意味着不能解压缩。任何地方生成的符合zlib标准的压缩数据都可以被其他符合zlib标准的解码器解压缩。你尝试过解压缩吗?

至于差异,@twotwotwo指出,Go中的compress/zlib并不是原始的zlib库,而是用Go语言编写的不同实现。因此,如果它使用不同的算法来查找匹配项和/或发出块,那么生成的输出就会有所不同。

英文:

Just because the compressed data is different doesn't mean that it can't be decompressed. zlib-compliant compressed data generated anywhere can be decompressed by a compliant zlib decoder anywhere else. Did you try decompressing?

As for the difference, @twotwotwo points out that compress/zlib in Go is not the original zlib library, but rather a different implementation written in Go. So it would be expected to generate different output if it uses different algorithms to find matches and/or emit blocks.

答案2

得分: 2

作为背景,Go语言没有自带的zlib库,而compress/zlib之所以被称为zlib,是因为它可以处理zlib格式的数据。尽管格式相同,但压缩算法的细节是不同的(例如,Go库的压缩速度稍慢且压缩效果稍差,详情请参考这里)。因此,即使在相同的压缩级别和相同的包装器下,输出通常也不会匹配。

zlib的作者Mark Adler在回答中提到了一个重要观点,即不同的输出并不意味着这些输出不能被zlib解压缩。我认为这是你真正需要的答案,但我还是保留了其他一些关于Go的信息和Go中的选项,因为它们包含了一些非重叠的信息。


Vitess项目(YouTube的内部MySQL代理)需要C语言zlib的速度,所以他们编写了一个适配器cgzip。你没有说明你希望以什么格式输出,如果答案不是gzip,你将需要fork并修改cgzip,以便它调用zlib的正确部分来生成你需要的内容。

使用cgzip或类似工具的好处是它会像zlib一样工作,因为它使用的就是zlib。缺点是你将不再拥有一个纯Go应用程序,因此你将失去轻松交叉编译的便利,并在构建和运行程序的环境中增加了对额外依赖的需求(尽管对于zlib来说,由于其广泛使用和稳定的API,将其作为依赖项并不是一个大问题)。

英文:

As background, Go doesn't come with the zlib library, and compress/zlib is only has its name 'cause it works with zlib format data. Though the format's the same, the details of the compression algorithm aren't (leading, for example, to worse speed and slightly worse compression for the Go library). So output won't typically match, even at the same compression level and with the same wrapper.

The answer from an author of zlib (Mark Adler) raises the essential point that different output doesn't mean the output can't be decompressed by zlib. I think that's your real answer here, but leaving the rest of this around because it has some non-overlapping information about what Go's doing/your options in Go.


The Vitess project (YouTube's internal MySQL proxy) needed the speed of C zlib for their application, so they wrote an adapter, cgzip. You didn't say what format you wanted the output in; if the answer is not gzip, you'll have to fork and modify cgzip so it calls the right bits of zlib to produce what you need.

The upside of using cgzip or similar is that it'll act like zlib because it's using zlib. The downside is that you'll no no longer have a pure-Go app, so you lose the trivial cross-compiles and gain an additional dependency on the environment your program's built and run in (though in zlib's case, the ubiquity and stable API mean it's much less of an issue to take it as a dep than some libraries).

huangapple
  • 本文由 发表于 2015年1月9日 10:28:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/27852940.html
匿名

发表评论

匿名网友

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

确定