title: In PHP, how to decompress a file on the fly that was compressed twice?
body: <p>I have a <code>bigfile.gz.gz</code> file that is… big. I would like to uncompress it on the fly. Ideally, this is what I have in mind:</p>
  
  <pre><code>$in = fopen('compress.zlib://compress.zlib://bigfile.gz.gz', 'rb');
  while (!feof($in))
      print fread($in, 4096);
  fclose($in);
  </code></pre>
  
  <p>However, <code>compress.zlib://</code> cannot be chained that way:</p>
  
  <pre><code>PHP Warning:  fopen(): cannot represent a stream of type ZLIB as a File Descriptor in gztest.php on line 1
  </code></pre>
  
  <h3> </h3>
  
  <p>So I thought I’d combine <code>gzopen()</code> and <code>compress.zlib://</code> together:</p>
  
  <pre><code>$in = gzopen('compress.zlib://bigfile.gz.gz', 'rb');
  while (!gzeof($in))
      print gzread($in, 4096);
  gzclose($in);
  </code></pre>
  
  <p>However, this only decompresses one level of gzip.</p>
  
  <h3> </h3>
  
  <p>I tried probably 10 other methods, unfortunately <code>gzopen()</code> does not work with <code>php://memory</code> if it’s been written to using <code>fwrite()</code>. And <code>stream_filter_append(… zlib.inflate …)</code> cannot read gzipped files.</p>
  
  <p>This is the best I could come up with, but it spawns two system processes, which has undesirable overhead:</p>
  
  <pre><code>$in = popen('zcat bigfile.gz.gz | gunzip', 'rb');
  while (!feof($in))
      print fread($in, 4096);
  fclose($in);
  </code></pre>
  
  <h3> </h3>
  
  <p>Can someone suggest something better maybe?</p>