Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set permissions (attributes) on a file in a ZIP file using Python's zipfile module?

When I extract files from a ZIP file created with the Python zipfile module, all the files are not writable, 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?

like image 360
Tom Avatar asked Jan 12 '09 06:01

Tom


People also ask

Does ZIP keep file permissions?

You cannot store Linux/Unix file permissions in a ZIP file. Edit (after comments) by using the "external attributes" field inside the ZIP header these attributes can be store inside a ZIP file. GNU's unzip is apparently able to read that additional field and restore file permissions.

What does ZIP file ZIP file do?

The ZIP file format is a common archive and compression standard. This module provides tools to create, read, write, append, and list a ZIP file.


1 Answers

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

buffer = "path/filename.zip"  # zip filename to write (or file-like object) name = "folder/data.txt"      # name of file inside zip  bytes = "blah blah blah"      # contents of file inside zip  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

like image 57
Tom Avatar answered Sep 30 '22 09:09

Tom