I am sending compressed file with piping either local or from a network location. And on the receiving end, I would like to detect the type of compression and use the appropriate decompression utility (gzip, bzip2, xz..etc) to extract it. Commands looks as follows:
Local:
cat misteryCompressedFile | [compressionUtility] -d -fc > /opt/files/uncompressedfile Over network:
ssh user@ipaddr "cat misteryCompressedFile" | [compressionUtility] -d -fc > /opt/files/uncompressedfile One can tell the type of compression used even if there is no extension provided (e.g., .gz or .bz2) by looking at first few hex values of the file. For example, if I use xxd to look at first few hex values of two compressed files, then I will 1f8b 0808 for gzip and 425a 6836 for bzip2.
However, to still use piping, how can I check the first incoming byte to select the proper decompression utility for the first of the file?
So if unknown compressed file is a gzip type, command will be this:
cat misteryCompressedFile | gzip -d -fc > /opt/files/uncompressedfile and if unknown compressed file is bzip2 type, command will be this:
cat misteryCompressedFile | bzip2 -d -fc > /opt/files/uncompressedfile Is it possible to make such decision with piping on the fly without having to download entire file and then make decision what to use for decompression?