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

使用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空间不够。
我们来看下压缩的代码:

b605fdeeef570ae348875570cbc16dd0.jpg

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压缩,具体的临界值是多少还没去仔细研究:

5830c7782651058a5d6cb9ef2bce8961.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容