14

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:\Python27\lib\zipfile.py", line 950, in extract
    return self._extract_member(member, path, pwd)
  File "C:\Python27\lib\zipfile.py", line 993, in _extract_member
    source = self.open(member, pwd=pwd)
  File "C:\Python27\lib\zipfile.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 Python27\Lib\zipfile.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.

0

3 Answers 3

17

Correct ZIP files always have "\x50\x4B\x03\x04" 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')
Sign up to request clarification or add additional context in comments.

6 Comments

@petr-viktorin Yes, it's a zip. The above code gave PK♥♦ as output.
Hm. Could you put your file on any server so I can look at it and try to open it?
Look at the original answer, I've posted another idea.
Hm, I can't extract pdf from your file using Ark. To be more correct, it extracts empty file which has size 0 KiB.
Python's zipfile currently supports only DEFLATE method. It seems you've created your file with WinRar using another method, so zipfile can only read file, but not extract.
|
3

Make sure you are really opening a ZIP file, not for example a RAR file named with a .zip extension. Proper zip files have a header, which was not found in this case.

The zipfile module can only open zip files. WinRAR can also open other formats, and it likely ignores the filename and only looks at the file itself.

Comments

0

I was using a third party c++ library to save data to a zip compressed numpy format for use within python. I had OP's exact error.

I found out I was overwriting parts within the zip file due to wrong size specifications of different data chunks.

Thus the header of the zip was correct but the contents messed up.

Make sure you check potential "manual" creation of the respective zip.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.