可能性の重複:
Deflate command line toolPHPの 'gzuncompress'関数はシェル内にありますか?
はい、私はシェル上でPHP自体を使用することができます知っているが、私はPHPが利用可能になる前に展開されているスクリプトでこの機能が必要。
私はgzip
とunzip
を試しましたが、私のパラメータが間違っているか、まったく同じ圧縮を使用していません。
私はこれをbashスクリプトで使いたいと思います。より高いレベルのスクリプト言語に進むことは選択肢ではありません。
私はテスト目的のために、以下のPHPスクリプトを書きました:
#!/usr/bin/php
<?
$contents = file_get_contents($argv[1]);
$data = gzuncompress($contents);
echo substr($data, 0, 20) . "\n";
?>
これは私が(復号化データの先頭に)期待するものを出力します。私はgunzip
に同じファイルを渡すと
:
$ gunzip -c data
gzip: data: not in gzip format
は私が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.
、私が行ってきましたrelated questionに提示された解決策で。
しかし、私は他のスクリプト言語への移動(および他の依存関係を導入する)避けたかったことを考えると、私はそれらのすべてと一緒に行きました:
_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
これらの4つのバージョンは私のテストケースとまったく同じ出力を生成します。
どのようにgzipを使用しますか? 'gzip -c/etc/motd |を試してみてください。ガンジー-c | diff/etc/motd -' – yarek
皮肉なことに、* gzip *と* zipは全く同じアルゴリズムを使用します。彼らはちょうど生データよりも多くを持っている。 –