#! /usr/bin/env python
# -*- coding: utf-8 -*- 

#===================================================================
# Ejercicio en: http://www.pythonchallenge.com/pc/def/channel.html
# Solución: hockey -> oxygen
# Obtenemos: http://www.pythonchallenge.com/pc/def/oxygen.html
#-------------------------------------------------------------------
# FJA - neocipres@telefonica.net            Junio de 2007
#===================================================================

import zipfile, re, sys 

traza = []
z = zipfile.ZipFile("channel.zip", "r")

def buscaZip( i ):
    nomf = i + ".txt"
    contf = z.read( nomf )
    n = re.findall( '[0-9]{1,5}$', contf )
    print "Fichero zip: ", nomf, " -> ", contf
    #tecla=raw_input('Pulse ENTER para ver el siguiente documento en el fichero zip.')
    if not n:
        resultado = ''
    else:
        resultado = n[0]
    return resultado
    
for nomf in z.namelist():
    bytes = z.read( nomf )
    print  nomf, " - ", len(bytes), " bytes", " - ", z.getinfo( nomf ).comment

#for info in z.infolist():    print info.filename, info.date_time, info.file_size, info.compress_size
#print z.read( z.namelist()[-1] )

ind = re.findall( '[0-9]{1,5}$', z.read( '90052.txt' )  )[0] 
while True:
    traza.append( ind )
    ind = buscaZip( ind )
    if ind == '':
        break
    
    
for t in traza:
    sys.stdout.write( z.getinfo( t + '.txt').comment )
    
z.close()
