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

查看:640
本文介绍了我如何使用Python的zip文件模块设置在一个ZIP文件的文件权限(属性)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从使用Python创建一个ZIP文件中提取文件 压缩文件 模块,所有文件都不可写,只读等。

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?

推荐答案

这似乎工作(感谢埃文,把它在这里太行是在上下文中):

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文件格式的说明:<一href=\"http://www.pkware.com/documents/casestudies/APPNOTE.TXT\">http://www.pkware.com/documents/casestudies/APPNOTE.TXT

这篇关于我如何使用Python的zip文件模块设置在一个ZIP文件的文件权限(属性)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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