'Check if file is actually a zipfile
I know I can use testzip
or just try to uncompress the file and catch the exception.
My issue is that I might have .msg
files which have attachments that are zipfiles.
In that case the ZipFile
will just skip over the first few bytes, open the attached zip file and not raise an exception.
The only way I can come up with this is manually opening the file and check the first two bytes to see if it's PK
, but I'd just like to have a more "strict" check.
Is this possible with either ZipFile
or another library?
Solution 1:[1]
What is "actually a zipfile" for you?
A file marked as such with PK
?
A valid "header" at the end of the file?
There are files in the wild that work as PE executable, ZIP, and PDF at the same time. The most strict you can get would probably both the PK and reading the header. You already answered your own question.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ypnos |