tags:

views:

832

answers:

3

When I extract files from a zip file created with the Python zipfile module, all the files are not writeable, read only etc.

The file is being created and extracted under Linux and Python 2.5.2.

As best I can tell, I need to set the ZipInfo.external_attr property for each file, but this doesn't seem to be documented anywhere I could find, can anyone enlighten me?

+3  A: 

Look at this: http://stackoverflow.com/questions/279945/set-permissions-on-a-compressed-file-in-python

I'm not entirely sure if that's what you want, but it seems to be.

The key line appears to be:

zi.external_attr = 0777 << 16L

It looks like it sets the permissions to 0777 there.

Evan Fosmark
thanks, it does have some hints, but not really an answer as such though...
Tom
+1  A: 

This seems to work (thanks Evan, putting it here so the line is in context):

zip = zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED)
info = zipfile.ZipInfo(name)
info.external_attr = 0777 << 16L # give full access to included file
zip.writestr(info, bytes)
zip.close()

I'd still like to see something that documents this... An additional resource I found was a note on the Zip file format: http://www.pkware.com/documents/casestudies/APPNOTE.TXT

Tom
A: 

When you do it like this, does it work alright?

zf = zipfile.ZipFile("something.zip")
for name in zf.namelist():
    f = open(name, 'wb')
    f.write(self.read(name))
    f.close()

If not, I'd suggest throwing in an os.chmod in the for loop with 0777 permissions like this:

zf = zipfile.ZipFile("something.zip")
for name in zf.namelist():
    f = open(name, 'wb')
    f.write(self.read(name))
    f.close()
    os.chmod(name, 0777)
Evan Fosmark
I'm not using Python to extract the zip, the zip is generated by a webserver and extracted using something on the user's machine. In my case the gnome archive manager program.
Tom