12

I have a 1.4GB zip file and am trying to yield each member in succession. The zipfile module keeps throwing a BadZipfile exception, stating that

"zipfile.BadZipfile: zipfiles that span multiple disks are not supported".

Here is my code:

import zipfile

def iterate_members(zip_file_like_object):
  zflo = zip_file_like_object
  assert zipfile.is_zipfile(zflo) # Here is where the error happens.
  # If I comment out the assert, the same error gets thrown on this next line:
  with zipfile.ZipFile(zflo) as zip:
    members = zip.namelist()
    for member in members:
      yield member

fn = "filename.zip"
iterate_members(open(fn, 'rb'))

I'm using Python 2.7.3. I tried on both Windows 8 and ubuntu with the same result. Any help very much appreciated.

jh314
  • 27,144
  • 16
  • 62
  • 82
user1541702
  • 131
  • 2
  • 5
  • Can you post the zip file (or a link to it)? The [code that leads up to this error](http://hg.python.org/cpython/file/2.7/Lib/zipfile.py#l176) is pretty straightforward; it checks whether the file header declares more than one disk or the disk number of the file to be anything other than zero. – phihag Jul 15 '13 at 21:34
  • Thanks phihag. Unfortunately I cannot post the file as it contains confidential client data. – user1541702 Jul 17 '13 at 19:18
  • Well, can you generate a zip file with large test data that still shows the problem? – phihag Jul 17 '13 at 19:32
  • Probably depends more on the software used to create the .zip. I just had this problem with a 500MB .zip from a customer. Unpacked it and repacked (obviously with a different zip tool than my customer) and it works. The repacked file is even bigger due to less compression. So, size does not seem to be all that matters. – Adrian W Jun 13 '18 at 08:08
  • `python3.7` can be run with a file while `python3.6` can't for me. – Lion Lai Jul 14 '21 at 07:48

2 Answers2

15

I get the same error on a similar file although I am using python 3.4

Was able to fix it by editing line 205 in zipfile.py source code:

if diskno != 0 or disks != 1:
    raise BadZipFile("zipfiles that span multiple disks are not supported")

to:

if diskno != 0 or disks > 1:

Hope this helps

Josselin
  • 151
  • 1
  • 3
  • 2
    The zip tool built into Windows seems buggy and procudes an end-of-archive record which looks like zip64, but isn't. Hence `_EndRecData64` finds `disks == 0`. It should have returend `endrec` instead. But the fix works nicely, thanks. – Adrian W Jun 13 '18 at 08:19
  • 8
    Any update on this? This does not seem like an acceptable solution since you need to manually modify the Python Library source code. What if the python installation runs in the cloud and you don't have access to the Python Installation. – Jean-Michel Provencher Nov 19 '18 at 15:33
2

Quick Fix, Install zipfile38 using:

pip install zipfile38

And use it in the code same as you are doing before

import zipfile38 as zipfile
#your code goes here