Tom*_*Tom 43 python zip attributes file-permissions zipfile
当我从使用Python zipfile模块创建的ZIP文件中提取文件时,所有文件都不可写,只读等.
该文件是在Linux和Python 2.5.2下创建和提取的.
我可以说,我需要ZipInfo.external_attr为每个文件设置属性,但这似乎没有记录在任何我能找到的地方,有人可以启发我吗?
Tom*_*Tom 42
这似乎有效(感谢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()
Run Code Online (Sandbox Code Playgroud)
我仍然希望看到记录此内容的东西...我发现的另一个资源是Zip文件格式的注释:http://www.pkware.com/documents/casestudies/APPNOTE.TXT
Fah*_*tha 24
此链接提供的信息比我在网上找到的任何其他内容都多.即使是拉链源也没有任何东西.复制相关部分以供后代使用.这个补丁实际上不是关于记录这种格式的,这只是为了说明当前文档是多么可悲(读取不存在).
# 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, '')
Run Code Online (Sandbox Code Playgroud)
此外,此链接具有以下内容.这里低位字节可能意味着四个字节中最右边(最低)的字节.所以这个是用于MS-DOS的,否则可以保留为零.
外部文件属性:(4个字节)
Run Code Online (Sandbox Code Playgroud)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的档案中下载的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.
*/
Run Code Online (Sandbox Code Playgroud)
所以把所有这些放在一起,看起来实际上只使用了第二高的字节,至少对于Unix来说.
编辑:我在Unix.SX上询问了Unix的这方面,问题是" zip格式的外部文件属性 ".看起来我有些不对劲.具体来说,前两个字节都用于Unix.
Eva*_*ark 13
看看这个:在python中设置压缩文件的权限
我不完全确定这是不是你想要的,但它似乎是.
关键线似乎是:
zi.external_attr = 0777 << 16L
Run Code Online (Sandbox Code Playgroud)
看起来它设置了0777那里的权限.
小智 7
早期的答案对我不起作用(在OS X 10.12上).我发现除了可执行标志(八进制755),我还需要设置"常规文件"标志(八进制100000).我在这里找到了这个:https://unix.stackexchange.com/questions/14705/the-zip-formats-external-file-attribute
一个完整的例子:
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()
Run Code Online (Sandbox Code Playgroud)
我的特定用例的完整示例,创建.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)
Run Code Online (Sandbox Code Playgroud)
此示例将默认文件权限更改为目录664并保留775。
相关代码: