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.
The format that
gzuncompressexpects 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
zpipecommand-line utility. The source code for this utility is found inexamples/zpipe.cin 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&gunzipdeal with) from PHP then you need to use gzencode & gzdecode instead of gzcompress & gzuncompress.