您的位置:首页 > Web前端

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

2017-04-27 15:54 330 查看

gzip算法

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

https://github.com/cscott530/sprite-kit-platformer/blob/master/SuperKoalio/LFCGzipUtility.m
http://www.clintharris.net/2009/how-to-gzip-data-in-memory-using-objective-c/
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐