How to change deflate stream output format(raw, zlib, gzip) when use zlib?

陌路散爱 提交于 2020-01-06 07:28:20

问题


Zlib can output three format, I try to search the docs and zlib.h, but can't find a clear explanation about the options, anyone have any ideas?


回答1:


From the zlib.h documentation of deflateInit2():

  windowBits can also be -8..-15 for raw deflate.  In this case, -windowBits
determines the window size.  deflate() will then generate raw deflate data
with no zlib header or trailer, and will not compute a check value.

  windowBits can also be greater than 15 for optional gzip encoding.  Add
16 to windowBits to write a simple gzip header and trailer around the
compressed data instead of a zlib wrapper.



回答2:


Fill in the blanks

int get_file_format(int n) {
  if      (n == 0) return 31;
  else if (n == 1) return 15;
  else if (n == 2) return -15;
  else if (n >= 9  && n <= 15) return n;  /* zlib with window size 2^9 to 2^15 */
  else if (n >= 25 && n <= 31) return n;  /* gzip with window size 2^9 to 2^15 */
  else if (n >= -15 && n <= -9)return n;  /* raw  with window size 2^9 to 2^15 */        
  else return Z_ERRNO;     
}

z_hist_sz = get_file_format(n);

ret = deflateInit2(&strm, COMPRESS_LEVEL, Z_DEFLATED, z_hist_sz ...)


来源:https://stackoverflow.com/questions/54663140/how-to-change-deflate-stream-output-formatraw-zlib-gzip-when-use-zlib

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!