如何使用 Python 的 zipfile 模块对 ZIP 文件中的文件设置权限(属性)?

问题描述

当我从使用 Python 创建的 ZIP 文件中提取文件时 zipfile 模块,所有文件不可写,只读等.

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

文件正在 Linux 和 Python 2.5.2 下创建和提取.

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

据我所知,我需要为每个文件设置 ZipInfo.external_attr 属性,但这似乎没有记录在我能找到的任何地方,谁能启发我?

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?


解决方案

这似乎可行(感谢 Evan,将其放在这里,以便该行符合上下文):

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()

我仍然希望看到记录此内容的内容...我发现的另一个资源是有关 Zip 文件格式的注释:http://www.pkware.com/documents/casestudies/APPNOTE.TXT

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

相关文章