title: Why is a different zlib window bits value required for extraction, compared with compression?
body: <p>I am trying to debug a problem with some code that uses <code>zlib</code> 1.2.8. The problem is that this larger project can make archives, but runs into <code>Z_DATA_ERROR</code> header problems when trying to extract that archive.</p>
  
  <p>To do this, I wrote a small test program in C++ that compresses ("deflates") a specified regular file, writes the compressed data to a second regular file, and extracts ("inflates") to a third regular file, one line at a time. I then <code>diff</code> the first and third files to make sure I get the same bytes. </p>
  
  <p>For reference, this test project is located at: <a href="https://github.com/alexpreynolds/zlib-test" rel="nofollow">https://github.com/alexpreynolds/zlib-test</a> and compiles under Clang (and should also compile under GNU GCC).</p>
  
  <p>My larger question is how to deal with header data correctly in my larger project.</p>
  
  <p>In my first test scenario, I can set up compression machinery with the following code:</p>
  
  <pre><code>z_error = deflateInit(this-&gt;z_stream_ptr, ZLIB_TEST_COMPRESSION_LEVEL);
  </code></pre>
  
  <p>Here, <code>ZLIB_TEST_COMPRESSION_LEVEL</code> is <code>1</code>, to provide best speed. I then run <code>deflate()</code> on the <code>z_stream</code> pointer until there is nothing left that comes out of compression.</p>
  
  <p>To extract these bytes, I can use <code>inflateInit()</code>:</p>
  
  <pre><code>int ret = inflateInit(this-&gt;z_stream_ptr);
  </code></pre>
  
  <p>So what is the header format, in this case?</p>
  
  <p>In my second test scenario, I set up the deflate machinery like so:</p>
  
  <pre><code>z_error = deflateInit2(this-&gt;z_stream_ptr, 
                         ZLIB_TEST_COMPRESSION_LEVEL, 
                         ZLIB_TEST_COMPRESSION_METHOD, 
                         ZLIB_TEST_COMPRESSION_WINDOW_BITS,
                         ZLIB_TEST_COMPRESSION_MEM_LEVEL,
                         ZLIB_TEST_COMPRESSION_STRATEGY);
  </code></pre>
  
  <p>These deflate constants are, respectively, <code>1</code> for level, <code>Z_DEFLATED</code> for method, <code>15+16</code> or <code>31</code> for window bits, <code>8</code> for memory level, and <code>Z_DEFAULT_STRATEGY</code> for strategy.</p>
  
  <p>The former <code>inflateInit()</code> call does not work; instead, I must use <code>inflateInit2()</code> and specify a modified window bits value:</p>
  
  <pre><code>int ret = inflateInit2(this-&gt;z_stream_ptr, ZLIB_TEST_COMPRESSION_WINDOW_BITS + 16);
  </code></pre>
  
  <p>In this case, the window bits value is not <code>31</code> as in the <code>deflateInit2()</code> call, but <code>15+32</code> or <code>47</code>. </p>
  
  <p>If I use <code>31</code> (or any other value than <code>47</code>), then I get a <code>Z_DATA_ERROR</code> on subsequent <code>inflate()</code> calls. That is, if I use the same window bits for the <code>inflateInit2()</code> call:</p>
  
  <pre><code>int ret = inflateInit2(this-&gt;z_stream_ptr, ZLIB_TEST_COMPRESSION_WINDOW_BITS);
  </code></pre>
  
  <p>Then I get the following error on attempting to <code>inflate()</code>:</p>
  
  <pre><code>Error: inflate to stream failed [-3]
  </code></pre>
  
  <p>Here, <code>-3</code> is the same as <code>Z_DATA_ERROR</code>.</p>
  
  <p>According to the documentation, using <code>31</code> with <code>deflateInit2()</code> should write a <code>gzip</code> header and trailer. Thus, <code>31</code> on the following <code>inflateInit2()</code> call should be expected to be able to extract the header information. </p>
  
  <p>Why is the modified value <code>47</code> working, but not <code>31</code>?</p>
  
  <p>My test project is mostly similar to the example code on the <code>zlib</code> site, with the exception of the extraction/inflation code, which inflates one <code>z_stream</code> chunk at a time and parses the output for newline characters. </p>
  
  <p>Is there something special about running <code>inflate()</code> only when a new buffer of extracted data is asked for — like header information going missing between <code>inflate()</code> calls — as opposed to running the whole extraction in one pass, as in the <code>zlib</code> example code?</p>
  
  <p>My larger debugging problem is looking for a robust way to extract a chunk of <code>zlib</code>-compressed data only on request, so that I can extract data one line at a time, as opposed to getting the whole extracted file. Something about the way I am handling the <code>zlib</code> format parameter seems to be messing me up, but I can't figure out why or how to fix this.</p>