`gzip` does work, but it only writes 16KiB compressed blocks (in my tests), and since your input compresses _really_ well, it takes a while to get there. If you run
while :; do echo "foobar"; sleep 1; done | gzip > file &
wait a little and then kill only the first part, you’ll get a valid compressed `file`.
If you want to test without killing anything, drop the `sleep`:
yes foobar | gzip > file &
After a few seconds you’ll see `file` increase to 16KiB.