How to gzip files "on fly" before copying

Hello,
I want to gzip some files before copying to remote host. There is no freespace on source host so it needs to be perfomed within one-liner. I tried the following but it didn't work

gzip -c -9 all_rvds.xml |ssh targethost "dd of=/tmp/all_rvds.xml.gz"
cat all_rvds.xml |gzip -c9 |ssh targethost "dd of=/tmp/all_rvds.xml.gz"

What is the right way?

The rarely-seen useful use of cat. No need to use dd.

gzip < inputfile | ssh username@host 'cat > outputfile'

You can transfer multiples by streaming tar.

tar -zcf - /path/to/files | ssh username@host 'tar -C /path/to/dest -zxf -
[35]adm@sourcehost:~> gzip < all_rvds.xml | ssh targethost "cat > /tmp/all_rvds.xml.gz"
##########
WELCOME MESSAGE
##########
adm@targethost's password:
bash: line 1: syntax error near unexpected token `)'
bash: line 1: �iP�][s�~�_��pK����D�%)G��-M1v%&i���9�I.^��*6)}h F�t�����nk_G������O4 ?�F��������.�w�O������������vt7�k����=�W��~6��?�~�<��H��^�f2��f��{2h,ʽ��~:�I�~��l4�y�}x{�m��e/'

Works fine here.

Something funny is going on with your remote host. You gave it a line of script to execute, there's no reason for it to try and execute the data you gave it as a script.

How to find out why it is going such way?

It would help to know what your system is, and what the system you're connecting to is.

You could try ssh username@host 'sh -c "command I want to execute"' to try and force it to not use stdin for commands...