我正在使用zlib来压缩文本数据流。文本数据以块形式显示,并且对于每个块,调用deflate()
,并将刷新设置为Z_NO_FLUSH
。一旦检索到所有块,则调用deflate()
并将flush设置为Z_FINISH
。zlib,deflate:要分配多少内存?
自然地,deflate()
在每次调用时都不会产生压缩输出。它在内部累积数据以实现高压缩率。那很好!每当deflate()
产生压缩输出时,该输出都附加到数据库字段 - 一个缓慢的过程。
但是,一旦deflate()
产生压缩数据,该数据可能不适合提供的输出缓冲区deflate_out
。因此需要拨打deflate()
的多个电话。这就是我想要的东西,以避免:
有没有一种方法,使
deflate_out
总是足够大,以便deflate()
可以存储在它所有的压缩数据,它决定产生输出每一个时间?
注:
未压缩数据的总大小是不事先知道的。如上所述,未压缩的数据以块的形式出现,压缩的数据也以块的形式附加到数据库字段。
在包含文件
zconf.h
中,我发现了以下评论。这可能是我在找什么?即(1 << (windowBits+2)) + (1 << (memLevel+9))
deflate()
可能产生的压缩数据的最大字节数是多少?/* The memory requirements for deflate are (in bytes): (1 << (windowBits+2)) + (1 << (memLevel+9)) that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values) plus a few kilobytes for small objects. For example, if you want to reduce the default memory requirements from 256K to 128K, compile with make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7" Of course this will generally degrade compression (there's no free lunch). The memory requirements for inflate are (in bytes) 1 << windowBits that is, 32K for windowBits=15 (default value) plus a few kilobytes for small objects. */
查看http://stackoverflow.com/questions/4936255/zlib-how-to-dimension-avail-out – nos 2012-01-17 23:17:00
@nos:这只是有用的,如果输入的大小是已知的。 – 2012-01-17 23:19:22
我读'zconf.h'中的注释是压缩的内存要求,而不是输出缓冲区的大小。 这就是说,它似乎是合乎逻辑,即一个上界输出缓冲器是总的内存要求(128K + 128K +“几千字节”在上面的例子)+标题长度(40字节)。 – 2012-01-17 23:29:51