如何用Python 2.4解压缩文件?

时间:2011-10-18 11:34:07

标签: python zip zipfile python-2.4

我很难弄清楚如何用2.4解压缩zip文件。 extract()未包含在2.4中。我只能在我的服务器上使用2.4.4。

有人可以提供一个简单的代码示例吗?

5 个答案:

答案 0 :(得分:51)

您必须使用namelist()extract()。考虑目录的示例

import zipfile
import os.path
import os
zfile = zipfile.ZipFile("test.zip")
for name in zfile.namelist():
  (dirname, filename) = os.path.split(name)
  print "Decompressing " + filename + " on " + dirname
  if not os.path.exists(dirname):
    os.makedirs(dirname)
  zfile.extract(name, dirname)

答案 1 :(得分:12)

Vinko的答案存在一些问题(至少在我运行时)。我得到了:

IOError: [Errno 13] Permission denied: '01org-webapps-countingbeads-422c4e1/'

以下是解决方法:

# unzip a file
def unzip(path):
    zfile = zipfile.ZipFile(path)
    for name in zfile.namelist():
        (dirname, filename) = os.path.split(name)
        if filename == '':
            # directory
            if not os.path.exists(dirname):
                os.mkdir(dirname)
        else:
            # file
            fd = open(name, 'w')
            fd.write(zfile.read(name))
            fd.close()
    zfile.close()

答案 2 :(得分:3)

修改Ovilia's answer,以便您也可以指定目标目录:

def unzip(zipFilePath, destDir):
    zfile = zipfile.ZipFile(zipFilePath)
    for name in zfile.namelist():
        (dirName, fileName) = os.path.split(name)
        if fileName == '':
            # directory
            newDir = destDir + '/' + dirName
            if not os.path.exists(newDir):
                os.mkdir(newDir)
        else:
            # file
            fd = open(destDir + '/' + name, 'wb')
            fd.write(zfile.read(name))
            fd.close()
    zfile.close()

答案 3 :(得分:1)

未完全测试,但应该没问题:

import os
from zipfile import ZipFile, ZipInfo

class ZipCompat(ZipFile):
    def __init__(self, *args, **kwargs):
        ZipFile.__init__(self, *args, **kwargs)

    def extract(self, member, path=None, pwd=None):
        if not isinstance(member, ZipInfo):
            member = self.getinfo(member)
        if path is None:
            path = os.getcwd()
        return self._extract_member(member, path)

    def extractall(self, path=None, members=None, pwd=None):
        if members is None:
            members = self.namelist()
        for zipinfo in members:
            self.extract(zipinfo, path)

    def _extract_member(self, member, targetpath):
        if (targetpath[-1:] in (os.path.sep, os.path.altsep)
            and len(os.path.splitdrive(targetpath)[1]) > 1):
            targetpath = targetpath[:-1]
        if member.filename[0] == '/':
            targetpath = os.path.join(targetpath, member.filename[1:])
        else:
            targetpath = os.path.join(targetpath, member.filename)
        targetpath = os.path.normpath(targetpath)
        upperdirs = os.path.dirname(targetpath)
        if upperdirs and not os.path.exists(upperdirs):
            os.makedirs(upperdirs)
        if member.filename[-1] == '/':
            if not os.path.isdir(targetpath):
                os.mkdir(targetpath)
            return targetpath
        target = file(targetpath, "wb")
        try:
            target.write(self.read(member.filename))
        finally:
            target.close()
        return targetpath

答案 4 :(得分:-1)

我在Python 2.7.3rc2中进行测试,而ZipFile.namelist()没有返回仅包含用于创建子目录的子目录名的条目,而只返回具有子目录的文件名列表,如下所示:

  

['20130923104558/control.json', '20130923104558/test.csv']

因此检查

  

if fileName == '':

根本不评估为True

因此,我修改了代码以检查dirName是否存在destDir内,并创建dirName(如果不存在)。仅当fileName部分不为空时才提取文件。因此,这应该考虑目录名称可以出现在ZipFile.namelist()

中的情况
def unzip(zipFilePath, destDir):
    zfile = zipfile.ZipFile(zipFilePath)
    for name in zfile.namelist():
        (dirName, fileName) = os.path.split(name)
        # Check if the directory exisits
        newDir = destDir + '/' + dirName
        if not os.path.exists(newDir):
            os.mkdir(newDir)
        if not fileName == '':
            # file
            fd = open(destDir + '/' + name, 'wb')
            fd.write(zfile.read(name))
            fd.close()
    zfile.close()