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

新手上路,请多包涵

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

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

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

原文由 Tom 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 840
2 个回答

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

 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

原文由 Tom 发布,翻译遵循 CC BY-SA 3.0 许可协议

此链接 提供的信息比我在网上找到的任何其他信息都多。甚至 zip 源也没有任何东西。为后代复制相关部分。这个补丁并不是真的要记录这种格式,这只是为了显示当前文档是多么可悲(读作不存在)。

 # external_attr is 4 bytes in size. The high order two
# bytes represent UNIX permission and file type bits,
# while the low order two contain MS-DOS FAT file
# attributes, most notably bit 4 marking directories.
if node.isfile:
    zipinfo.compress_type = ZIP_DEFLATED
    zipinfo.external_attr = 0644 << 16L # permissions -r-wr--r--
    data = node.get_content().read()
    properties = node.get_properties()
    if 'svn:special' in properties and \
           data.startswith('link '):
        data = data[5:]
        zipinfo.external_attr |= 0120000 << 16L # symlink file type
        zipinfo.compress_type = ZIP_STORED
    if 'svn:executable' in properties:
        zipinfo.external_attr |= 0755 << 16L # -rwxr-xr-x
    zipfile.writestr(zipinfo, data)
elif node.isdir and path:
    if not zipinfo.filename.endswith('/'):
        zipinfo.filename += '/'
    zipinfo.compress_type = ZIP_STORED
    zipinfo.external_attr = 040755 << 16L # permissions drwxr-xr-x
    zipinfo.external_attr |= 0x10 # MS-DOS directory flag
    zipfile.writestr(zipinfo, '')

此外, 此链接 具有以下内容。这里的低位字节大概是指四个字节中最右边(最低)的字节。所以这个是针对 MS-DOS 的,否则可能会保留为零。

外部文件属性:(4字节)

>        The mapping of the external attributes is
>       host-system dependent (see 'version made by').  For
>       MS-DOS, the low order byte is the MS-DOS directory
>       attribute byte.  If input came from standard input, this
>       field is set to zero.
>
> ```

此外,从 [Debian 档案](http://packages.debian.org/source/stable/zip) 下载的 InfoZIP zip 程序源代码中的源文件 unix/unix.c 在注释中有以下内容。

/* lower-middle external-attribute byte (unused until now): * high bit => (have GMT mod/acc times) >>> NO LONGER USED! <<< * second-high bit => have Unix UID/GID info * NOTE: The high bit was NEVER used in any official Info-ZIP release, * but its future use should be avoided (if possible), since it * was used as “GMT mod/acc times local extra field” flags in Zip beta * versions 2.0j up to 2.0v, for about 1.5 years. */

”`

所以把所有这些放在一起,看起来实际上只使用了第二高的字节,至少对于 Unix 是这样。

编辑:我在 Unix.SX 上的“ Zip 格式的外部文件属性”问题中询问了 Unix 方面的问题。看起来我做错了几件事。特别是前两个字节都用于 Unix。

原文由 Faheem Mitha 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题