Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a file is gzip compressed?

I have a C / C++ program which needs to read in a file that may or may not be gzip compressed. I know we can use gzread() from zlib to read in both compressed and uncompressed files - however, I want to use the zlib functions ONLY if the file is gzip compressed (for performance reasons).

So is there any way to programatically detect or check if a certain file is gzipped from C / C++?

like image 874
Deepak Prakash Avatar asked May 19 '11 13:05

Deepak Prakash


People also ask

How can I tell if a file is gzip?

Usually though, you just look at the file extension, like on Windows. Like . ZIP means ZIP file, . gz means gzip.

How do I see gzip compression?

Verify Your Compression In your browser: In Chrome, open the Developer Tools > Network Tab (Firefox/IE will be similar). Refresh your page, and click the network line for the page itself (i.e., www.google.com ). The header “Content-encoding: gzip” means the contents were sent compressed.

How do you know if a file has been compressed?

Most compressed files have extensions indicating the format like . zip, . rar, . 7z, etc.

Is .GZ compressed file?

The GZ file format is commonly used as a compression format for Linux and Unix operating systems.


2 Answers

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.

like image 162
Bruno Rohée Avatar answered Oct 07 '22 17:10

Bruno Rohée


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.

like image 33
pmg Avatar answered Oct 07 '22 17:10

pmg