linux (Un/De)在bash中压缩字符串?

j8ag8udp  于 5个月前  发布在  Linux
关注(0)|答案(2)|浏览(53)

在bash中可以使用stdin/stdout压缩/删除字符串吗?
我试过了,但显然不支持。

hey=$(echo "hello world" | gzip -cf)
echo $hey # returns a compressed string
echo $hey | gzip -cfd
gzip: stdin is a multi-part gzip file -- not supported

字符串
我不是很精通Linux,但我读了其他压缩实用程序手册页,找不到解决方案?

slwdgvem

slwdgvem1#

如果33%的压缩率损失对你来说是可以接受的,那么你可以存储base64编码的压缩数据:

me$mybox$ FOO=$(echo "Hello world" | gzip | base64 -w0) # compressed, base64 encoded data
me$mybox$ echo $FOO | base64 -d | gunzip # use base64 decoded, uncompressed data
Hello world

字符串
它将工作,但每3(压缩)字节将存储在4字节的文本。

rqmkfv5c

rqmkfv5c2#

当您这样做时:

hey=$(echo "hello world" | gzip -cf)

字符串
你在变量hey中没有相同的字节,因为你在/tmp/myfile中创建了:

echo "hello world" | gzip -cf > /tmp/myfile


你得到“gzip:stdin is a multi-part gzip file -- not supported”错误,仅仅是因为你破坏了无法解压缩的压缩数据。
VAR=$(...)结构是为处理文本而设计的。这就是为什么你会得到额外的尾部修剪。

相关问题