ZipFile and file rigths

P

perchef

hi,

i have written this small code which allow me to unzip files using
python :

import zipfile
import os
import os.path

pathDir="/home/toto/Desktop"
pathZip= os.path.join(pathDir,"foobar.zip")

if zipfile.is_zipfile(pathZip):
zf = zipfile.ZipFile(pathZip)

for file in zf.namelist():
newPath = os.path.join(pathDir,file)
print newPath
if not file.endswith('/') and not os.path.exists(newPath) :
newFile = open(newPath, 'wb')
newFile.write(zf.read(file))
newFile.flush()
newFile.close()
else :
os.makedirs(newPath)

zf.close()
else:
print pathZip + " is not a zip file"

it works well but i have a small problem : i can't see how i can deal
with file rights.
When I unzip files with this script all the unzipped files haven't the
good rights.
for example :
with this script :
-rw-r--r-- foo.exe
with a traditional zip program (ie : stuffit ):
-rwxr-xr-x foo.exe
ZipInfo objects doesn't store informations about rights ?
(http://www.python.org/doc/current/lib/zipinfo-objects.html#zipinfo-objects)

How can i fix this ?
 
R

ralobao

You can use os.chmod to fix it.

Here the description:

os.chmod

Docstring:
chmod(path, mode)

Change the access permissions of a file.

Cheers,

perchef escreveu:
 
S

Sybren Stuvel

perchef enlightened us with:
it works well but i have a small problem : i can't see how i can
deal with file rights. When I unzip files with this script all the
unzipped files haven't the good rights.

That's right. Why should they? ZIP doesn't store file permissions.
ZipInfo objects doesn't store informations about rights ?
Correct.

How can i fix this ?

Don't use ZIP. Use tar instead.

Sybren
 
R

Robert Kern

perchef said:
hi,

i have written this small code which allow me to unzip files using
python :

import zipfile
import os
import os.path

pathDir="/home/toto/Desktop"
pathZip= os.path.join(pathDir,"foobar.zip")

if zipfile.is_zipfile(pathZip):
zf = zipfile.ZipFile(pathZip)

for file in zf.namelist():
newPath = os.path.join(pathDir,file)
print newPath
if not file.endswith('/') and not os.path.exists(newPath) :
newFile = open(newPath, 'wb')
newFile.write(zf.read(file))
newFile.flush()
newFile.close()
else :
os.makedirs(newPath)

zf.close()
else:
print pathZip + " is not a zip file"

it works well but i have a small problem : i can't see how i can deal
with file rights.
When I unzip files with this script all the unzipped files haven't the
good rights.
for example :
with this script :
-rw-r--r-- foo.exe
with a traditional zip program (ie : stuffit ):
-rwxr-xr-x foo.exe
ZipInfo objects doesn't store informations about rights ?
(http://www.python.org/doc/current/lib/zipinfo-objects.html#zipinfo-objects)

How can i fix this ?

This is possibly related, I'm not sure:

http://article.gmane.org/gmane.comp.python.apple/8012

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
P

perchef

ZIP doesn't store file permissions.
ok, but in that case how could stuffit retrieve these permissions ?

Thanks for the link Robert.
 
R

Robert Kern

perchef wrote:

[Sybren Stuvel:]
Yes, the .zip file format does store file permissions appropriate to the
platform that generates the file.
ok, but in that case how could stuffit retrieve these permissions ?

Thanks for the link Robert.

When one makes a .zip file using ZipFile, the permissions are stored,
but there is also a flag that tells unzip utilities what system the .zip
file was made on. ZipFile always stores this flag as MS Windows no
matter what system actually made it. StuffIt seems to ignore the flag
and always propagate the permissions when it unzips. It assumes that the
..zip file was made on a Mac. InfoZip's unzip(1) utility does look at the
flag, and if it says "MS Windows", it does not propagate the
permissions. If the .zip file actually *were* made on a Windows machine
and unzipped on a UNIX-like filesystem, all of the files would be marked
executable. I remember this used to happen quite often a few years ago.
InfoZip made an arguably incorrect choice (but also a fairly convenient
one), but ZipFile's behavior is definitely incorrect.

OTOH, I don't know if this has any relevance to the problem that you are
seeing.

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
S

Sybren Stuvel

Robert Kern enlightened us with:
Yes, the .zip file format does store file permissions appropriate to
the platform that generates the file.

I never knew that! Thanks for correcting me ;-)

Sybren
 
P

perchef

OTOH, I don't know if this has any relevance to the problem that you are seeing.

not really, i have used the 'os.chmod' trick, but it's still
interesting.
thanks.
 

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
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top