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

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Reading corrupted/partial zip files in python

First a simple script for reading non-corruted zipfiles 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.
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS