archiving symlinks with python zipfile

后端 未结 3 2052
野性不改
野性不改 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)
    
    0 讨论(0)
  • 2021-01-18 07:28

    zipfile doesn't appear to support storing symbolic links. The way to store them in a ZIP is actually not part of the format and is only available as a custom extension in some implementations. In particular, Info-ZIP's implementation supports them so you can delegate to it instead. Make sure your decompression software can handle such archives - as I said, this feature is not standardized.

    0 讨论(0)
  • 2021-01-18 07:41

    It is possible to have zipfile store symbolic links, instead of the files themselves. For an example, see here. The relevant part of the script is storing the symbolic link attribute within the zipinfo:

    zipInfo = zipfile.ZipInfo(archiveRoot)
    zipInfo.create_system = 3
    # long type of hex val of '0xA1ED0000L',
    # say, symlink attr magic...
    zipInfo.external_attr = 2716663808L
    zipOut.writestr(zipInfo, os.readlink(fullPath))
    
    0 讨论(0)
提交回复
热议问题