zlib error while attempting compression: "Ran out of output buffer for writing compressed bytes."

来源:互联网 发布:co网络用语是什么意思 编辑:程序博客网 时间:2024/05/24 06:19

gzip算法

使用Object-C代码来实现gzip的压缩功能,不管网上找的哪个地方的代码,方法都是一样的

  1. https://github.com/cscott530/sprite-kit-platformer/blob/master/SuperKoalio/LFCGzipUtility.m
  2. http://www.clintharris.net/2009/how-to-gzip-data-in-memory-using-objective-c/
  3. http://v2it.win/ios/gzip%E6%96%87%E4%BB%B6%E5%8E%8B%E7%BC%A9%E5%92%8C%E8%A7%A3%E5%8E%8B%E7%BC%A9/

在使用大的数据进行gzip压缩时,一切正常,但是使用小的数据,比如几个字节,这是就会出现以下的错误:
zlib error while attempting compression: "Ran out of output buffer for writing compressed bytes."

错误的原因是buffer空间不够。


我们来看下压缩的代码:



gzip压缩时,分配的压缩后的数据空间大小为: 1.01倍 + 12.
// Create output memory buffer for compressed data. The zlib documentation states that
// destination buffer size must be at least 0.1% larger than avail_in plus 12 bytes.
NSMutableData compressedData = [NSMutableData dataWithLength:[pUncompressedData length]1.01 + 12];

针对小的数据,这个大小远远不够,如果改成 10倍 + 12,可以正常gzip压缩,具体的临界值是多少还没去仔细研究:


参考:

1. http://www.jianshu.com/p/a0c94dc75efc


0 0
原创粉丝点击