How to check if a STDIO stream is gzipped and consequently gunzip it in the same stream

Solution 1:

Use zless instead of zcat. If its input isn't compressed, it will pass it through unchanged (this allows you to use a single run of zless with a mixture of compressed and uncompressed files). It's normally an interactive utility, but if the output is not a terminal it's just a filter.

echo "My plain text" | gzip | gzip | zless | zless