简体   繁体   English

无法在Python中解压缩文件夹

[英]Can't unzip a folder in Python

I tried unzipping a file through Python using zipfile.extractAll but it gave BAD zip file, hence I tried this: 我尝试使用zipfile.extractAll通过Python解压缩文件,但它给出了BAD zip文件,因此我尝试了以下操作:

zipfile cant handle some type of zip data? zipfile无法处理某些类型的zip数据?

As mentioned in this answer, i used the code: 如该答案中所述,我使用了以下代码:

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  enter code here

but it gave the error 但它给了错误

Message File Name Line Position Traceback 消息文件名行位置回溯
C:\\Users\\aditya1.r\\Desktop\\Python_pyscripter\\module1.py 50 C:\\ Users \\ aditya1.r \\ Desktop \\ Python_pyscripter \\ module1.py 50
main C:\\Users\\aditya1.r\\Desktop\\Python_pyscripter\\module1.py 17 主C:\\ Users \\ aditya1.r \\ Desktop \\ Python_pyscripter \\ module1.py 17
fixBadZipfile C:\\Users\\aditya1.r\\Desktop\\Python_pyscripter\\module1.py 37 fixBadZipfile C:\\ Users \\ aditya1.r \\ Desktop \\ Python_pyscripter \\ module1.py 37
TypeError: 'str' does not support the buffer interface TypeError:“ str”不支持缓冲区接口

I'm using Python 3.4 我正在使用Python 3.4

How can i unzip this file? 如何解压缩该文件?

please go through this link this might be what you are looking for 请通过此链接,这可能是您要寻找的

Unzipping files in python 在python中解压缩文件

You are reading the file as a bytes object but trying to find passing a string object so just simply change this line - 您正在将文件读取为bytes对象,但尝试查找传递string对象,因此只需更改此行-

pos = data.find('\x50\x4b\x05\x06')

to

pos = data.find(b'\x50\x4b\x05\x06') 

Note that I have casted it to a byte object by simply prepending a b . 请注意,我已经通过简单地在b前面添加了一个对象。

You don't need to do this is Python 2.X but in python 3.X you need to explicitly serialize a string object to a byte object. 您不需要使用Python 2.X,但是在python 3.X中,您需要将string对象显式序列化为byte对象。

import subprocess

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

Hope this help you :) 希望这对您有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM