deciding what is a dir (folder) in a zip file

J

Jim

Hello,

I'm trying to read .zip files and drop from the listing those files
that are directories. I'm using the zipfile module.

Does anyone know how I can I tell which files in the .zip are
directories? I have looked around the net for the information but I
cannot make it out: the pkzip specification hints that it is in the
"external file attribute" but also hints that it is dependent on the
platform on which the .zip was made (I get .zips from lots of
platforms). The info-zip mailing list is unfortunately swamped by
spam, so I can't ask there. 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.

(I could os.system("unzip "+fn) and search the disk for directories but
I'd rather not, for reasons I won't list.)

I'd be grateful for any help,
Jim
 
G

Gabriel Genellina

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top