dongzhi9457 2019-07-12 14:07
浏览 127
已采纳

压缩的JSON大于未压缩的版本

I'll try to clear up my question.

myJSON is a simple JSON string. len(myJSON) = 78

e is json.Marshal(myJSON)

From what I understand, e is now a []byte

Then I gzip e like this:

var buf bytes.Buffer
gz := gzip.NewWriter(&buf)
gz.Write(e)
gz.Close()

And buf.Len() = 96

So... why is my compressed buffer bigger than the original non-compressed string?

Edit: It's hilarious the trolls that down vote a question when someone is trying to understand WHY something is happening. Guess I should just blindly accept it and not ask.

  • 写回答

2条回答 默认 最新

  • doushang1778 2019-07-12 14:27
    关注

    gzip will add a header and make some changes to the original data. For the case, the original data is really small it will not guarantee compressed data will smaller than original data.

    So if your program will constantly deal with the small data like this. Compress data use compress library may not a good idea. Some time we serialize the data into binary stream for the case that data is constantly small.

    Go gzip package ref:

    Package gzip implements reading and writing of gzip format compressed files, as specified in RFC 1952.

    RFC1952

    gzip format and header:

    http://www.onicos.com/staff/iz/formats/gzip.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)