简体   繁体   English

gzip 在 bash 中对 STRING 进行编码/放气,然后在 php 中对 STRING 进行解码/充气

[英]gzip encode/deflate STRING in bash, then decode/inflate STRING in php

I wish to produce a gzip'ed STRING in bash, and then send it to a php script for decoding/inflating.我希望在 bash 中生成一个 gzip'ed STRING,然后将其发送到一个 php 脚本进行解码/膨胀。 But I am coming across a problem with, I think, the gzip'ed string produced by gzip in bash.但是我遇到了一个问题,我认为,在 bash 中由 gzip 生成的 gzip 字符串。

    # from 
    # https://stackoverflow.com/questions/7538846/un-decompress-a-string-in-bash#7539075
    FOO="$(echo "Hello world" | gzip -c | base64)" # compressed, base64 encoded data
    echo $FOO | base64 -d | gunzip # use base64 decoded, uncompressed data
    echo '--'

    # unpack base64 again before sending to php
    FOO="$(echo $FOO | base64 -d)"

    # this is where it goes wrong:
    # php doesn't seem to like the gzip string format.

    # gzuncompress produces:
    #   PHP Warning:  gzuncompress(): data error in Command line code on line 1
    php -r "echo gzuncompress('$FOO') . \"\n\";"
    echo '--'

    # gzdecode produces:
    #   PHP Warning:  gzdecode(): data error in Command line code on line 1
    php -r "echo gzdecode('$FOO') . \"\n\";"
    echo '--'

    # gzinflate produces:
    #   PHP Warning:  gzinflate(): data error in Command line code on line 1
    php -r "echo gzinflate('$FOO') . \"\n\";"
    echo '--'

I am suspecting that the problem may be with the bash gzip format.我怀疑问题可能出在 bash gzip 格式上。
But I am unable to see any options that might correct this.但是我看不到任何可以纠正此问题的选项。

All thoughts appreciated.所有的想法都值得赞赏。

The gzipped string is not safe in a bash string, if i run如果我运行,gzip 压缩的字符串在 bash 字符串中是不安全的

FOO="$(echo "Hello world" | gzip -c | base64)"
FOO="$(echo "$FOO" | base64 -d)"

I get an error我收到一个错误

-bash: warning: command substitution: ignored null byte in input

But it works if I leave the string base64 encoded like this using base64_decode() / gzdecode() :但是,如果我使用base64_decode() / gzdecode()保留这样编码的字符串 base64 ,它会起作用:

FOO="$(echo "Hello world" | gzip -c | base64)"
php -r "echo gzdecode(base64_decode('$FOO'));"

Output:输出:

Hello world

It also works if you pipe the output of the decoded string to PHP:如果您将解码字符串的输出通过管道传输到 PHP,它也可以工作:

FOO="$(echo "Hello world" | gzip -c | base64)"
echo "$FOO" | base64 -d | php -r 'echo gzdecode(file_get_contents("php://stdin"));'

(example to read from stdin taken from here ) (从这里获取的标准输入读取的示例)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM