doutidi5037 2018-10-24 23:03
浏览 182
已采纳

我如何使用zlib在C中压缩并在golang中解压缩

So I have been trying to convert some data from C to be compressed and sent to a library which is in golang where it will be decompressed. only issue is, they seem to be slightly different implementations from each other.

What i did was to use "zlib.h" library from C to compress the string "hello" which resulted in bytes of

[120 156 203 72 205 201 201 103 32 5]

while in golang for the same string "hello" results in an array of

[120 156 202 72 205 201 201 7 4 0 0 255 255 6 44 2 21]

My question is if there is a way to make these outputs similar , what is it? or at the very least can a compressed data in C be decompressed in zlib?

I've also looked at How can I use zlib in golang to cooperate with zlib in c? but i'm looking for something more specific like an example.

  • 写回答

1条回答 默认 最新

  • douyuan3842 2018-10-24 23:38
    关注

    Comparing compressed data tells you nothing. Different compressors, or different versions of the same compressor, or the same version used with different settings, can all give different compressed output for the same input. What actually matters for a lossless compressor is whether you can decompress to the original data.

    The problem with your first example is that it is not complete. (The second example is complete and correct.) The first example ends in the middle of a deflate block. There is an error in your usage of zlib, either in managing the resulting data or not properly requesting the completion of the compression.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python验证码滑块图像识别
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)