title: PHP's 'gzuncompress' function in the shell?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/3178566/deflate-command-line-tool">Deflate command line tool</a>  </p>
  </blockquote>
  
  
  
  <p>Yes, I know I can use PHP itself on the shell, but I need this functionality in a script that is deployed before PHP is available.</p>
  
  <p>I've tried <code>gzip</code> and <code>unzip</code> but either my parameters are incorrect or they plain just don't use the same compression.</p>
  
  <p>I want to use this in a bash script. Going into a higher level scripting language isn't an option.</p>
  
  <hr>
  
  <p>I wrote the following PHP script for testing purposes:</p>
  
  <pre><code>#!/usr/bin/php
  &lt;?
    $contents = file_get_contents( $argv[1] );
    $data = gzuncompress( $contents );
    echo substr( $data, 0, 20 ) . "\n";
  ?&gt;
  </code></pre>
  
  <p>This outputs what I expect (the beginning of the decoded data).</p>
  
  <p>If I pass the same file to <code>gunzip</code>:</p>
  
  <pre><code>$ gunzip -c data
  gzip: data: not in gzip format
  </code></pre>
  
  <p>If I try <code>unzip</code>:</p>
  
  <pre><code>$ unzip data
  Archive:  data
    End-of-central-directory signature not found.  Either this file is not
    a zipfile, or it constitutes one disk of a multi-part archive.  In the
    latter case the central directory and zipfile comment will be found on
    the last disk(s) of this archive.
  unzip:  cannot find zipfile directory in one of data2 or
          data2.zip, and cannot find data2.ZIP, period.
  </code></pre>
  
  <hr>
  
  <p>As suggested, and given that there is no other practical way, I went with the solution presented in the <a href="https://stackoverflow.com/questions/3178566/deflate-command-line-tool">related question</a>.<br>
  But given that I wanted to avoid moving to another scripting language (and introducing another dependency) I went with all of them:</p>
  
  <pre><code>_uncompressedData=
  if hash perl 2&gt;&amp;-; then
    echo "Using Perl for decompression..." &gt;&amp;2
    _uncompressedData=$(perl -MCompress::Zlib -e 'undef $/; print uncompress(&lt;&gt;)' &lt; compressed.bin || true)
  elif hash ruby 2&gt;&amp;-; then
    echo "Using Ruby for decompression..." &gt;&amp;2
    _uncompressedData=$(ruby -rzlib -e 'print Zlib::Inflate.new.inflate(STDIN.read)' &lt; compressed.bin || true)
  elif hash php 2&gt;&amp;-; then
    echo "Using PHP for decompression..." &gt;&amp;2
    _uncompressedData=$(php -r "echo gzuncompress(file_get_contents('php://stdin'));" &lt; compressed.bin || true)
  elif hash python 2&gt;&amp;-; then
    echo "Using Python for decompression..." &gt;&amp;2
    _uncompressedData=$(python -c "import zlib,sys;print zlib.decompress(sys.stdin.read())" &lt; compressed.bin || true)
  else
    echo "Unable to find decompressor!" &gt;&amp;2
    exit 1
  fi
  </code></pre>
  
  <p>These 4 versions produced the exact same output with my test cases.</p>