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
1.2k views
in Technique[技术] by (71.8m points)

unzip - Bad magic number error with ZipFile module in Python

I am using Python 2.7 on Windows 7 (64 bit). When I try to unzip a zip file with ZipFile module I get the following error:-

Traceback (most recent call last):
  File "unzip.py", line 8, in <module>
    z.extract(name)
  File "C:Python27libzipfile.py", line 950, in extract
    return self._extract_member(member, path, pwd)
  File "C:Python27libzipfile.py", line 993, in _extract_member
    source = self.open(member, pwd=pwd)
  File "C:Python27libzipfile.py", line 897, in open
    raise BadZipfile, "Bad magic number for file header"
zipfile.BadZipfile: Bad magic number for file header

WinRAR could extract the file I am trying to extract just fine. Here is the code I used to extract files from myzip.zip

from zipfile import ZipFile
z = ZipFile('myzip.zip')   //myzip.zip contains just one file, a password protected pdf        
for name in z.namelist():
    z.extract(name)

This code is working fine for many other zip files I created using WinRAR but myzip.zip

I tried commenting the following lines in Python27Libzipfile.py:-

if fheader[0:4] != stringFileHeader:
   raise BadZipfile, "Bad magic number for file header"

But this didn't really help. Running my code with this in effect, I get some dump on my shell.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Correct ZIP files always have "x50x4Bx03x04" in the beginning. You can test whether file is really ZIP file with this code:

with open('/path/to/file', 'rb') as MyZip:
  print(MyZip.read(4))

It will print header of file so you can check.

UPDATE Strange, testzip() and all other functions work good. Had you tried such code?

with zipfile.GzipFile('/path/to/file') as Zip:
  for ZipMember in Zip.infolist():
    Zip.extract(ZipMember, path='/dir/where/to/extract', pwd='your-password')

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

...