Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Reading corrupted/partial zip files in python (See related posts)

First a simple script for reading non-corruted zipfiles in python:

filename = 'foo.zip'

import zipfile

z = zipfile.ZipFile(filename)

for i in z.infolist():
    print i.filename, i.file_size

z.read('somefile')


Next we use 'zip -FF foo.zip' to fix the zipfile, before reading it:

filename = 'foo.zip'

import zipfile

try:
    z = zipfile.ZipFile(filename)
except zipfile.BadZipfile:
    import commands
    commands.getoutput('zip -FF '+filename)
    z = zipfile.ZipFile(filename)

for i in z.infolist():
    print i.filename, i.file_size

try: 
    z.read('somefile')
except zipfile.BadZipfile:
    print 'Bad CRC-32'



In short: use 'zip -FF file.zip' to fix the file. It will restore the filelist.

You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts