Reading corrupted/partial zip files in python
1 2 filename = 'foo.zip' 3 4 import zipfile 5 6 z = zipfile.ZipFile(filename) 7 8 for i in z.infolist(): 9 print i.filename, i.file_size 10 11 z.read('somefile')
Next we use 'zip -FF foo.zip' to fix the zipfile, before reading it:
1 2 filename = 'foo.zip' 3 4 import zipfile 5 6 try: 7 z = zipfile.ZipFile(filename) 8 except zipfile.BadZipfile: 9 import commands 10 commands.getoutput('zip -FF '+filename) 11 z = zipfile.ZipFile(filename) 12 13 for i in z.infolist(): 14 print i.filename, i.file_size 15 16 try: 17 z.read('somefile') 18 except zipfile.BadZipfile: 19 print 'Bad CRC-32'
In short: use 'zip -FF file.zip' to fix the file. It will restore the filelist.