How do I set permissions (attributes) on a file in a ZIP file using Python#39;s zipfile module?(如何使用 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
这篇关于如何使用 Python 的 zipfile 模块对 ZIP 文件中的文件设置权限(属性)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Python 的 zipfile 模块对 ZIP 文件中的文件设置权限(属性)?
基础教程推荐
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 包装空间模型 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 求两个直方图的卷积 2022-01-01
