1

Possible Duplicate:
Deflate command line tool

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.

I've tried gzip and unzip but either my parameters are incorrect or they plain just don't use the same compression.

I want to use this in a bash script. Going into a higher level scripting language isn't an option.


I wrote the following PHP script for testing purposes:

#!/usr/bin/php
<?
  $contents = file_get_contents( $argv[1] );
  $data = gzuncompress( $contents );
  echo substr( $data, 0, 20 ) . "\n";
?>

This outputs what I expect (the beginning of the decoded data).

If I pass the same file to gunzip:

$ gunzip -c data
gzip: data: not in gzip format

If I try unzip:

$ 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.

As suggested, and given that there is no other practical way, I went with the solution presented in the related question.
But given that I wanted to avoid moving to another scripting language (and introducing another dependency) I went with all of them:

_uncompressedData=
if hash perl 2>&-; then
  echo "Using Perl for decompression..." >&2
  _uncompressedData=$(perl -MCompress::Zlib -e 'undef $/; print uncompress(<>)' < compressed.bin || true)
elif hash ruby 2>&-; then
  echo "Using Ruby for decompression..." >&2
  _uncompressedData=$(ruby -rzlib -e 'print Zlib::Inflate.new.inflate(STDIN.read)' < compressed.bin || true)
elif hash php 2>&-; then
  echo "Using PHP for decompression..." >&2
  _uncompressedData=$(php -r "echo gzuncompress(file_get_contents('php://stdin'));" < compressed.bin || true)
elif hash python 2>&-; then
  echo "Using Python for decompression..." >&2
  _uncompressedData=$(python -c "import zlib,sys;print zlib.decompress(sys.stdin.read())" < compressed.bin || true)
else
  echo "Unable to find decompressor!" >&2
  exit 1
fi

These 4 versions produced the exact same output with my test cases.

2
  • 1
    How do you use gzip? Try gzip -c /etc/motd | gunzip -c | diff /etc/motd -
    – yrk
    Commented Feb 7, 2012 at 21:54
  • Ironically, both gzip and zip use exactly the same algorithm. They just happen to have more than the raw data. Commented Feb 7, 2012 at 21:58

1 Answer 1

9

The format that gzuncompress expects is the raw zlib format without the gzip header. This is the format you get by default if you use the zlib library's C API.

You can uncompress this format using the zpipe command-line utility. The source code for this utility is found in examples/zpipe.c in the zlib source code distribution. But this program is not compiled and installed by default. There are no widely deployed command-line utilities that accept the raw zlib format.

If you want to use the gzip format instead (the one with the friendly header which gzip & gunzip deal with) from PHP then you need to use gzencode & gzdecode instead of gzcompress & gzuncompress.

2
  • Sadly, I do not control the compressed data. Commented Feb 7, 2012 at 22:24
  • 1
    Then I'm afraid that, even though you said it wasn't an option, you have to consider bundling a utility like zpipe or using one of the scripting languages to do the decompression. The Deflate command line tool question linked to by Ignacio Vazquez-Abrams suggests methods for Python, Ruby, and Perl. Plus you know how to do it in PHP.
    – Celada
    Commented Feb 7, 2012 at 22:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.