繁体   English   中英

ZipFile无法提取包含目录的zip

[英]ZipFile can't extract a zip with directories

我正在尝试用Python解压缩文件。 我正在使用以下功能:

def unzip(source_filename, dest_dir):
    zf = zipfile.ZipFile(source_filename)
    for member in zf.infolist():
        # Path traversal defense copied from
        # http://hg.python.org/cpython/file/tip/Lib/http/server.py#l789
        words = filter(None, member.filename.split('/'))
        path = dest_dir
        for word in words[:-1]:
            drive, word = os.path.splitdrive(word)
            head, word = os.path.split(word)
            if word in (os.curdir, os.pardir): continue
            path = os.path.join(path, word)
        zf.extract(member, path)

使用名为“ gui”的目录解压缩.zip时,出现以下错误:

Traceback (most recent call last):
  File "MCManager.py", line 137, in add
    unzip(addedFilepath, dirUnzipped)
  File "MCManager.py", line 19, in unzip
    zf.extract(member, path)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/zipfile.py", line 928, in extract
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/zipfile.py", line 962, in _extract_member
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.py", line 157, in makedirs
OSError: [Errno 20] Not a directory: '/Users/student/Library/Application Support/minecraft/temp/unzipped/gui/gui'

ZipFile()是否有问题?

查看方法解extract文档 ,您会发现方法通过变量dest_dir接收到的path变量必须是您希望将迭代的zip文件的内容解压缩到的dir的路径。

正如您在问题上所说的, gui是文件而不是目录。 老实说,您声明

使用名为“ gui(...)”的文件解压缩.zip时

没什么意义。

另外,您可能需要看一下os.mkdir方法,以便可以按需创建目录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM