def repair_central_directory(zipFile, is_file_instance):
 f = (zipFile if is_file_instance else open(zipFile, 'rb+'))
 data = f.read()
 pos = data.find(CENTRAL_DIRECTORY_SIGNATURE)
 if (pos > 0):
  sio = BytesIO(data)
  sio.seek((pos + 22))
  sio.truncate()
  sio.seek(0)
  return sio
 f.seek(0)
 return f

def repair_central_directory ( zipFile , is_file_instance ) : f = ( zipFile if is_file_instance else open ( zipFile , 'rb+' ) ) data = f . read ( ) pos = data . find ( CENTRAL_DIRECTORY_SIGNATURE ) if ( pos > 0 ) : sio = BytesIO ( data ) sio . seek ( ( pos + 22 ) ) sio . truncate ( ) sio . seek ( 0 ) return sio f . seek ( 0 ) return f
trims trailing data from the central directory code taken from URL courtesy of uri cohen .