By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,931 Members | 2,015 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,931 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Nov 16 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
Nov 16 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.