Python 从长路径提取文件时 Windows Zipfile extractall IOError

Python Zipfile extractall IOError on Windows when extracting files from long paths

我是运行 python zipfile extractall,它解压到一个超过255个字符的路径。 运行 这在 windows 7 64 位上。 我遇到以下错误 [Errno 2] No such file or directory: u'

有什么想法吗?

这是我要提取的网络文件夹 from/to。所以我将文件夹挂载为网络驱动器 t:\ 这暂时解决了问题。

这对我有用:

class ZipfileLongPaths(zipfile.ZipFile):

    def _extract_member(self, member, targetpath, pwd):
        targetpath = winapi_path(targetpath)
        return zipfile.ZipFile._extract_member(self, member, targetpath, pwd)

其中 winapi_path 是:

def winapi_path(dos_path, encoding=None):
    path = os.path.abspath(dos_path)

    if path.startswith("\\"):
        path = "\\?\UNC\" + path[2:]
    else:
        path = "\\?\" + path 

    return path  

winapi_path 摘自 pathname too long to open?