How to check if a file is gzip compressed?

后端 未结 4 1777
太阳男子
太阳男子 2020-12-03 13:15

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 uncompress

4条回答
  •  悲&欢浪女
    2020-12-03 13:53

    You can test for the signatures described in the RFCs 1951 and 1952 to get an idea. For GZIP files the second one is the relevant and it is definitive. There are some false positives on other formats, so you should check as much of the header for plausible values.

    For just zlib streams it's somewhat harder, because they are even more prone to false positives. But you would rarely encounter those in the wild on their own.

提交回复
热议问题