Daniel Hornik
Daniel Hornik

Reputation: 2521

Gunzip from string

I have one problem. I'd like to decompress string directly from a file. I have one script in bash that create another script.

#!/bin/bash


echo -n '#!/bin/bash 
' > test.sh #generate header for interpreter
echo -n "echo '" >> test.sh #print echo to file
echo -n "My name is Daniel" | gzip -f >> test.sh #print encoded by gzip string into a file
echo -n "' | gunzip;" >> test.sh #print reverse commands for decode into a file
chmod a+x test.sh #make file executable

I want to generate script test.sh that will the shortest script. I'm trying to compress string "My name is Daniel" and write it directly into file test.sh

But if I run test.sh i got gzip: stdin has flags 0x81 -- not supported Do you know why have I got this problem?

Upvotes: 1

Views: 3621

Answers (2)

Daniel Hornik
Daniel Hornik

Reputation: 2521

The problem was with storing null character (\0) in bash script. Null character can not be stored in echo and variable string. It can be stored in files and pipes.

I want to avoid use base64, but I fixed it with

printf "...%b....%b" "\0" "\0"

I edited script with bless hex editor. It's working for me :)

Upvotes: 0

Nahuel Fouilleul
Nahuel Fouilleul

Reputation: 19315

gzip output is binary so it can contain any character, as script is generated with bash it contains characters which are encoded (echo $LANG).

characters which cause problem between single quotes are NUL 0x0, ' 0x27 and non ascii characters 128-256 0x80-0xff.

a solution can be to use ANSI C quotes $'..' and to escape NUL and non ascii characters.

EDIT bash string can't contain nul character :

gzip -c <<<"My name is Daniel" | od -c -tx1 

trying to create ansi string

echo -n $'\x1f\x8b\x08\x00\xf7i\xe2Y\x00\x03\xf3\xadT\xc8K\xccMU\xc8,VpI\xcc\xcbL\xcd\^C1\x00\xa5u\x87\xad\x11\x00\x00\x00' | od -c -tx1

shows that string is truncated after nul character.

The best compromise may be to use base64 encoding:

gzip <<<"My name is Daniel"| base64

base64 --decode <<__END__ | gzip -cd
H4sIAPts4lkAA/OtVMhLzE1VyCxWcEnMy0zN4QIAgdbGlBIAAAA=
__END__ 

or

base64 --decode <<<H4sIAPts4lkAA/OtVMhLzE1VyCxWcEnMy0zN4QIAgdbGlBIAAAA=|gzip -cd

Upvotes: 5

Related Questions