zipfile cant handle some type of zip data?

前端 未结 3 878
深忆病人
深忆病人 2021-01-02 09:54

I came up over this problem while trying to decompress a zip file.

-- zipfile.is_zipfile(my_file) always returns False, even though the UNIX command unz

3条回答
  •  长情又很酷
    2021-01-02 10:23

    Run into the same issue on my files and was able to solve it. I'm not sure how they were generated, like in the above example. They all had trailing data in the end ignored by both Windows by 7z and failing python's zipfile.

    This is the code to solve the issue:

    def fixBadZipfile(zipFile):  
         f = open(zipFile, 'r+b')  
         data = f.read()  
         pos = data.find('\x50\x4b\x05\x06') # End of central directory signature  
         if (pos > 0):  
             self._log("Truncating file at location " + str(pos + 22) + ".")  
             f.seek(pos + 22)   # size of 'ZIP end of central directory record' 
             f.truncate()  
             f.close()  
         else:  
             # raise error, file is truncated  
    

提交回复
热议问题