Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
104 views
in Technique[技术] by (71.8m points)

python - Identify if a file is a gzip file


I need to check using C++ if a file being opened is a gzip file or not.
In Python I use the following code to identify if a file is gzipped:
test_file = "junk.txt.gz"
with open(test_file, "rb") as f:
   f_read_first_two_bytes = f.read(2)
   if f_read_first_two_bytes==b'x1fx8b':
      print("The file is a gzipped file", end='
')

What is the equivalent in C++?

I am new to C++ and tried the following but that obviously is not the right way.

int main() {
        char p[3] = {0};
        p[2] = '
';
        // open the junk.txt.gz file. We do not want to just go by the '.gz' in the file name.
        // but want to check just like the way we did in the Python code.
        ifstream is("./junk.txt.gz", std::ios::in|std::ios::out|std::ios::binary);
        //read  two characters into p
        is.read(p,2);
        cout << std::hex << p[0] << " " << std::hex << p[1] << endl;
        return 0;
}
question from:https://stackoverflow.com/questions/65839155/identify-if-a-file-is-a-gzip-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

but that obviously is not the right way.

Well obviously not, since you don't compare the bytes with anything. Otherwise, it pretty much is "right" as much as the Python program is.

A simple way to do the comparison is to interpret the bytes as unsigned char:

auto up = reinterpret_cast<unsigned char*>(p);
if (up[0] == 0x1f && up[1] == 0x8b)

P.S. This is not necessarily the most accurate test for gzip files. It can have false positives.

I recommend not attempting to implement the test manually. There are open source libraries for this purpose (like there are for most purposes).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...