zipfile无法处理某些类型的zip数据?

时间:2011-02-07 15:35:58

标签: python zipfile

我在尝试解压缩zip文件时遇到了这个问题。

- zipfile.is_zipfile(my_file)总是返回False,即使UNIX命令unzip处理它也没问题。此外,在尝试zipfile.ZipFile(path/file_handle_to_path)时,我得到了相同的错误

- file命令返回Zip archive data, at least v2.0 to extract并在其显示的文件上使用less

PKZIP for iSeries by PKWARE Length Method Size Cmpr Date Time CRC-32 Name 2113482674 Defl:S 204502989 90% 2010-11-01 08:39 2cee662e myfile.txt 2113482674 204502989 90% 1 file

任何想法如何解决这个问题?如果我可以让python的zipfile工作,那将是很好的,因为我已经有一些单元测试,如果我将切换到运行subprocess.call("unzip")

我将不得不放弃

3 个答案:

答案 0 :(得分:5)

在我的文件上运行相同的问题,并能够解决它。我不确定它们是如何生成的,就像上面的例子一样。它们最终都被Windows 7x和python的zipfile失败而忽略了尾随数据。

这是解决问题的代码:

def fixBadZipfile(zipFile):  
     f = open(zipFile, 'r+b')  
     data = f.read()  
     pos = data.find('\x50\x4b\x05\x06') # End of central directory signature  
     if (pos > 0):  
         self._log("Truncating file at location " + str(pos + 22) + ".")  
         f.seek(pos + 22)   # size of 'ZIP end of central directory record' 
         f.truncate()  
         f.close()  
     else:  
         # raise error, file is truncated  

答案 1 :(得分:1)

你说在它显示的文件上使用less等等。你是说这个吗?

less my_file

如果是这样,我猜这些是zip程序放入的注释 文件。查看我在网上找到的iSeries PKZIP用户指南, 这似乎是默认行为。

zipfile的文档说“此模块目前不处理ZIP 附加评论的文件。“也许这就是问题? 当然,如果less显示它们,这似乎暗示它们是 prepended ,FWIW。)

看来你(或者在iSeries机器上创建zipfile的人) 可以使用ARCHTEXT(*NONE)将其关闭,或使用ARCHTEXT(*CLEAR) 将其从现有的zip文件中删除。

答案 2 :(得分:0)

# Utilize mmap module to avoid a potential DoS exploit (e.g. by reading the
# whole zip file into memory). A bad zip file example can be found here:
# https://bugs.python.org/issue24621

import mmap
from io import UnsupportedOperation
from zipfile import BadZipfile

# The end of central directory signature
CENTRAL_DIRECTORY_SIGNATURE = b'\x50\x4b\x05\x06'


def repair_central_directory(zipFile):
    if hasattr(zipFile, 'read'):
        # This is a file-like object
        f = zipFile
        try:
            fileno = f.fileno()
        except UnsupportedOperation:
            # This is an io.BytesIO instance which lacks a backing file.
            fileno = None
    else:
        # Otherwise, open the file with binary mode
        f = open(zipFile, 'rb+')
        fileno = f.fileno()
    if fileno is None:
        # Without a fileno, we can only read and search the whole string
        # for the end of central directory signature.
        f.seek(0)
        pos = f.read().find(CENTRAL_DIRECTORY_SIGNATURE)
    else:
        # Instead of reading the entire file into memory, memory-mapped the
        # file, then search it for the end of central directory signature.
        # Reference: https://stackoverflow.com/a/21844624/2293304
        mm = mmap.mmap(fileno, 0)
        pos = mm.find(CENTRAL_DIRECTORY_SIGNATURE)
        mm.close()
    if pos > -1:
        # size of 'ZIP end of central directory record'
        f.truncate(pos + 22)
        f.seek(0)
        return f
    else:
        # Raise an error to make it fail fast
        raise BadZipfile('File is not a zip file')