archiving symlinks with python zipfile

后端 未结 3 2053
野性不改
野性不改 2021-01-18 07:14

I have a script that creates zip files of dirs containing symlinks. I was surprised to find that the zipfiles have zipped the targets of the links as opposed to the links th

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-18 07:25

    I have defined the following method in a Zip support class

    def add_symlink(self, link, target, permissions=0o777):
        self.log('Adding a symlink: {} => {}'.format(link, target))
        permissions |= 0xA000
    
        zi = zipfile.ZipInfo(link)
        zi.create_system = 3
        zi.external_attr = permissions << 16
        self.zip.writestr(zi, target)
    

提交回复
热议问题