473,473 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
1 1266
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: vinesh | last post by:
I have sample Asp.Net Web Application project. Let me know how to keep the files related to this project (like the webform.aspx, WebForm1.aspx.vb, WebForm1.aspx.resx) in a separate folder within a...
9
by: Paul | last post by:
I'm trying to make get my app to delete all the files in a specified folder and all the files within the folders of the specified folder. e.g. Folder 1 contains three files (File1, File2, File3)...
2
by: Fred Mertz | last post by:
I'd like to know the rationalle some of you use for deciding which members of System.IO you use to do your file I/O. It appears that there are many ways to accomplish any task (reading from a text...
2
by: Fred Mertz | last post by:
I'd like to know the rationalle some of you use for deciding which members of System.IO you use to do your file I/O. It appears that there are many ways to accomplish any task (reading from a text...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.