def winapi_path(dos_path):
import os
path = os.path.abspath(dos_path)
if path.startswith("\\\\"):
path = "\\\\?\\UNC\\" + path[2:]
else:
path = "\\\\?\\" + path
return path
class MyTarFile(tarfile.TarFile):
def _extract_member(self, tarinfo, targetpath):
targetpath = winapi_path(targetpath)
tarfile.TarFile._extract_member(self, tarinfo, targetpath)
tarfile.open = MyTarFile.open
tar_file = tarfile.open('your file path')
tar_file.extractall(r'c:/test_dir')
tar_file.close()
主要原因是因为dos的路径字符长度有260的上限, 在解压之前, 使用我上面的代码, 原理就是覆盖tarfile中解压的部分, 判断路径, 如果路径过长, 就替换为windows可识别的过长路径