如何使用 Python 的 zipfile 模块对 ZIP 文件中的文件设置权限(属性)? [英] How do I set permissions (attributes) on a file in a ZIP file using Python's zipfile module?

查看:44
本文介绍了如何使用 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 文件中的文件设置权限(属性)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆