如何在 Python 3.4 中使用带有“跟随符号链接”功能的 tar+gzip 压缩?问题是:
- tarfile.open() 支持 "w:gz" 模式但不支持 "dereference" 选项
- tarfile.tarfile() 支持“取消引用”但不支持“w:gz”模式
代码:
...
mode = ""
if bckentry['method'] == "tar":
mode = "w"
elif bckentry['method'] == "targz":
mode = "w:gz"
archive = tarfile.TarFile(name=filepath, mode=mode)
archive.dereference = True if bckentry['followsym'] == "yes" else False
# archive = tarfile.open(filepath, mode=mode)
if bckentry['withpath'] == 'yes':
for entry in bckentry['include_dirs']:
archive.add(entry, filter=self.filter_tar)
elif bckentry['withpath'] == 'no':
for entry in bckentry['include_dirs']:
archive.add(entry, arcname=os.path.basename(entry), filter=self.filter_tar)
...