title: ZLIB inflate give 'data error' in PHP
body: <p>I've got a file that has zlib <code>deflate</code>d blocks of 4096 bytes. I'm able to inflate at least 1 block of 4096 bytes with C++, using Minzip's <code>inflate</code> implementation, without garbled text or <code>data error</code>.</p>
  
  <p>I'm using the following C++ implementation to inflate the data:</p>
  
  <pre><code>#define DEC_BUFFER_LEN 20000
  int main(int argc, char* argv[]) {
      FILE *file = fopen("unpackme.3di", "rb");
  
      char *buffer = new char[4096];
  
      std::fstream outputFile;
      outputFile.open("output.txt", std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);
  
  
      // Data zit nu in de buffer
      char *decbuffer = new char[DEC_BUFFER_LEN];
  
      mz_streamp streampie = new mz_stream();
  
      streampie-&gt;zalloc = Z_NULL;
      streampie-&gt;zfree = Z_NULL;
      streampie-&gt;opaque = Z_NULL;
      streampie-&gt;avail_in = Z_NULL;
      streampie-&gt;next_in = Z_NULL;
  
      if (inflateInit(streampie) != Z_OK)
          return -1;
  
      fread(buffer, 1, 4096, file);
  
      streampie-&gt;next_in = (Byte *)&amp;buffer[0];
      streampie-&gt;avail_in = 4096;
  
      streampie-&gt;next_out = (Byte *)&amp;decbuffer[0];
      streampie-&gt;avail_out = DEC_BUFFER_LEN;
  
      streampie-&gt;total_out = 0;
  
      int res = inflate(streampie, Z_NO_FLUSH);
  
      if (res != Z_OK &amp;&amp; res != Z_STREAM_END) {
          std::cout &lt;&lt; "Error: " &lt;&lt; streampie-&gt;msg &lt;&lt; std::endl;
          return;
      }
      outputFile.write(decbuffer, streampie-&gt;total_out); // Write data to file
  
      fclose(file);
  
      inflateEnd(streampie);
  
      outputFile.flush();
      outputFile.close();
  
      getchar();
  
      return 0;
  }
  </code></pre>
  
  <p>and I'm using the following PHP implementation:</p>
  
  <pre><code>function Unpack3DI($inputFilename) {
      $handle = fopen($inputFilename, 'rb');
      if ($handle === false) return null;
  
      $data = gzinflate(fread($handle, 4096));
      return $data;
  }
  
  
  var_dump(Unpack3DI('unpackme.3di'));
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>Warning: gzinflate() [function.gzinflate]: data error in /var/www/html/3di.php on line 9
  bool(false)
  </code></pre>