无法在 Python 中解压缩文件夹



我尝试使用zipfile.extractall通过python解压缩文件,但它给出了不良的zip文件,因此我尝试了此:

Zipfile无法处理某种类型的zip数据?

如下所述,我使用了代码:

def fixBadZipfile(zipFile):  
     f = open(zipFile, 'r+b')  
     data = f.read()  
     pos = data.find('x50x4bx05x06') # 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  enter code here

,但它给出了错误

消息文件名称线位置追溯
C: users aditya1.r desktop python_pyscripter module1.py 50
主C: users aditya1.r desktop python_pyscripter module1.py 17
FixBadzipfile C: Users Aditya1.r Desktop python_pyscripter module1.py 37
typeError:'str'不支持缓冲区接口

我正在使用Python 3.4

如何解压缩此文件?

import subprocess
subprocess.Popen('unzip ' + file_name, shell = True).wait()

希望这对您有帮助:)

您正在将文件读为bytes对象,但要找到传递string对象,因此只需更改此行 -

pos = data.find('x50x4bx05x06')

to

pos = data.find(b'x50x4bx05x06') 

请注意,我只需准备b就将其铸造为字节对象。

您不需要这样做是Python 2.x,但是在Python 3.X中,您需要将string对象明确化为byte对象。

最新更新