当我提取的文件从一个压缩文件的创建与蟒蛇 zipfile 模块,所有文件不可写的、只读等等。

该文件是在创建和取在Linux和Python2.5.2.

作为最好我可以告诉大家,我需要设置 ZipInfo.external_attr 酒店的每个文件,但这似乎并没有被记录下任何地方我能找到的,任何人都可以启发我吗?

有帮助吗?

解决方案

这似乎有效(感谢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

其他提示

此链接提供的信息比我以前的任何其他内容都多能在网上找到。即使是拉链源也没有任何东西。复制相关部分以供后代使用。这个补丁实际上并不是关于记录这种格式的,这只是为了说明当前文档是多么可悲(读取不存在)。

# 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.

另外,InfoZIP的zip程序源代码中的源文件unix / unix.c,从下载Debian的档案在评论中有以下内容。

  /* 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上询问了Unix方面的问题,问题是<!>“; zip格式的外部文件属性 <!> quot;。看起来我有些不对劲。具体来说,前两个字节都用于Unix。

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

我不完全确定这是不是你想要的,但似乎是。

关键线似乎是:

zi.external_attr = 0777 << 16L

看起来它将权限设置为0777那里。

之前的答案对我不起作用(在OS X 10.12上)。我发现除了可执行标志(八进制755)之外,我还需要设置<!>“常规文件<!>”;旗帜(八进制100000)。我在这里找到了这个: https://unix.stackexchange.com/questions/ 14705 /所述拉链的格式外部的文件属性

一个完整的例子:

zipname = "test.zip"
filename = "test-executable"

zip = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED)

f = open(filename, 'r')
bytes = f.read()
f.close()

info = zipfile.ZipInfo(filename)
info.date_time = time.localtime()
info.external_attr = 0100755 << 16L

zip.writestr(info, bytes, zipfile.ZIP_DEFLATED)

zip.close()

我的特定用例的完整示例,创建.app的zip,以便文件夹Contents/MacOS/中的所有内容都是可执行的: https://gist.github.com/Draknek/3ce889860cea4f59838386a79cc11a85

你可以延长 ZipFile 类更改的默认文件的权限:

from zipfile import ZipFile, ZipInfo
import time

class PermissiveZipFile(ZipFile):
    def writestr(self, zinfo_or_arcname, data, compress_type=None):
        if not isinstance(zinfo_or_arcname, ZipInfo):
            zinfo = ZipInfo(filename=zinfo_or_arcname,
                            date_time=time.localtime(time.time())[:6])

            zinfo.compress_type = self.compression
            if zinfo.filename[-1] == '/':
                zinfo.external_attr = 0o40775 << 16   # drwxrwxr-x
                zinfo.external_attr |= 0x10           # MS-DOS directory flag
            else:
                zinfo.external_attr = 0o664 << 16     # ?rw-rw-r--
        else:
            zinfo = zinfo_or_arcname

        super(PermissiveZipFile, self).writestr(zinfo, data, compress_type)

该示例的变化的默认文件的权限 664 并保持 775 对于目录。

相关代码:

当你这样做的时候,它能正常工作吗?

zf = zipfile.ZipFile("something.zip")
for name in zf.namelist():
    f = open(name, 'wb')
    f.write(self.read(name))
    f.close()

如果没有,我建议在for循环中使用0777权限投入os.chmod,如下所示:

zf = zipfile.ZipFile("something.zip")
for name in zf.namelist():
    f = open(name, 'wb')
    f.write(self.read(name))
    f.close()
    os.chmod(name, 0777)

另请参阅 Python的zipfile模块

def write(self, filename, arcname=None, compress_type=None):
    ...
    st = os.stat(filename)
    ...
    zinfo = ZipInfo(arcname, date_time)
    zinfo.external_attr = (st[0] & 0xFFFF) << 16L      # Unix attributes
    ...

```

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top