dsoihsnz85757 2015-01-09 02:28
浏览 212

如何在golang中使用zlib与c中的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?

  • 写回答

2条回答 默认 最新

  • douzhi2012 2015-01-09 02:51
    关注

    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).

    评论

报告相同问题?