相关疑难解决方法(0)

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

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

该文件是在Linux和Python 2.5.2下创建和提取的.

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

python zip attributes file-permissions zipfile

43
推荐指数
5
解决办法
2万
查看次数

在python中设置压缩文件的权限

我有一个test.txtzip存档内的文件test.zip.test.txt当压缩时,权限不受我的控制,但现在我希望它们可以进行组写.我正在使用Python解压缩文件,并且不想逃避到shell.

编辑: 这是我到目前为止所得到的:

import zipfile

z = zipfile.ZipFile('test.zip', 'w')
zi = zipfile.ZipInfo('test.txt')
zi.external_attr = 0777 << 16L
z.writestr(zi, 'FOO')
z.close()

z = zipfile.ZipFile('test.zip', 'r')
for name in z.namelist():
    newFile = open(name, "wb")
    newFile.write(z.read(name))

    newFile.close()
z.close()
Run Code Online (Sandbox Code Playgroud)

这在使用2.5.1的OS X上完美运行,但它在我的主页框(Debian,Python 2.4和2.5)或使用Python 2.4的RHEL 5上不起作用.除了OS X之外,它不会出错,但也不会更改权限.有什么想法吗?另外,writestr()工作怎么样?我知道我在这里错误地使用它.

有没有办法在没有的情况下执行此操作os.chmod(提取文件的用户在提取os.chmod后没有权限使用)?我对zip文件有完全写入权限.

更多信息:

> ls -l test.zip
-rwxrwxrwx 1 myuser mygroup 2008-11-11 13:24 test.zip
> unzip test.zip
Archive:  test.zip
  inflating: test.txt 
> ls -l test.txt
-rw-r--r-- 1 myuser …
Run Code Online (Sandbox Code Playgroud)

python zip zipfile

7
推荐指数
2
解决办法
4338
查看次数

标签 统计

python ×2

zip ×2

zipfile ×2

attributes ×1

file-permissions ×1