How to check if a file is gzip compressed?
There is a magic number at the beginning of the file. Just read the first two bytes and check if they are equal to 0x1f8b
.
Do you prefer false positives, false negatives, or no false results at all (there goes performance down the drain...)?
The RFC 1952: GZIP file format specification version 4.3 states the first 2 bytes (of each member and therefore) of the file are '\x1F'
and '\x8B'
. Use that for a first check that can result in false positives.