deciding what is a dir (folder) in a zip file
Gabriel Genellina
gagsl-py at yahoo.com.ar
Thu Nov 16 18:41:02 EST 2006
At Thursday 16/11/2006 10:48, Jim wrote:
>I'm trying to read .zip files and drop from the listing those files
>that are directories. I'm using the zipfile module.
>
>Googling has increased my suspicion that
>it is not just a matter of seeing if the file name ends in '/' and that
>the relevant external file attribute seems to have different values for
>people from different platforms, so just experimenting on my platform
>doesn't seem to be a safe solution.
Ending in / appears to be enough. Filenames are stored using / in
Windows or Linux, using winzip, gzip and winrar at least.
Notice that no ordering is implied: directories may come _after_
filenames inside it.
This fragment extracts all ZipFile contents, maintaining directory structure:
--- cut ---
zf = ZipFile(zfname, 'r')
try:
# creo todos los subdirectorios que existen en el zip
# (vienen en cualquier orden)
namelist = list(zf.namelist())
namelist.sort(key=lambda n:len(n))
for fn in namelist:
if fn.endswith('/'):
fullfn = os.path.join(tmpdir, *fn[:-1].split('/'))
if not os.path.isdir(fullfn):
os.makedirs(fullfn)
# extraigo todos los archivos, respetando sus directorios
for fn in namelist:
if fn.endswith('/'):
continue
fullfn = os.path.join(tmpdir, *fn.split('/'))
ofile = open(fullfn, 'wb')
ofile.write(zf.read(fn))
ofile.close()
# mantener fecha/hora
dt = zf.getinfo(fn).date_time
ft = time.mktime((dt[0],dt[1],dt[2],dt[3],dt[4],dt[5],0,0,-1))
os.utime(fullfn, (ft,ft))
finally:
zf.close()
--- cut ---
--
Gabriel Genellina
Softlab SRL
__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
More information about the Python-list
mailing list