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

Python zipfile无法处理某些类型的zip数据?,python,zipfile,Python,Zipfile,我在解压zip文件时遇到了这个问题 --zipfile.is_zipfile(我的文件)总是返回False,即使UNIX命令unzip处理得很好。另外,当尝试执行zipfile.zipfile(path/file\u handle\u to\u path)时,我会遇到相同的错误 --file命令返回Zip存档数据,至少v2.0以提取,并在显示的文件上使用less: PKWARE提供的iSeries的PKZIP 长度方法大小Cmpr日期时间CRC-32名称 2113482674定义:S 20450

我在解压zip文件时遇到了这个问题

--
zipfile.is_zipfile(我的文件)
总是返回False,即使UNIX命令unzip处理得很好。另外,当尝试执行
zipfile.zipfile(path/file\u handle\u to\u path)
时,我会遇到相同的错误

--
file
命令返回
Zip存档数据,至少v2.0以提取
,并在显示的文件上使用
less

PKWARE提供的iSeries的PKZIP 长度方法大小Cmpr日期时间CRC-32名称 2113482674定义:S 204502989 90%2010-11-01 08:39 2cee662e myfile.txt 2113482674 204502989 90%1文件


你知道我该怎么处理这个问题吗?如果我能让python的
zipfile
正常工作,那就太好了,因为我已经有了一些单元测试,如果我要切换到运行
子流程,我将不得不放弃这些单元测试。你是说这个吗

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

zipfile
的文档说“此模块当前不处理ZIP “也许这就是问题所在?(关于 当然,如果
less
显示它们,这似乎意味着它们是 预加,FWIW。)

看起来是您(或在iSeries机器上创建zipfile的人) 可以使用
ARCHTEXT(*NONE)
关闭此功能,或使用
ARCHTEXT(*CLEAR)
关闭此功能
将其从现有zipfile中删除。

在我的文件中遇到相同的问题,并且能够解决它。我不确定它们是如何生成的,就像上面的例子一样。它们都有尾随数据,最终被Windows和失败的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  

导入os
并告诉我们,os.path.exists(my_文件)
返回的是什么。@eumiro:True。为什么?:)谢谢@hyperboreean-我想你可能会对文件名有问题,因为
zipfile.is_zipfile
对不存在的文件也返回
False
。听起来你可能会有类似的问题。不幸的是,该海报没有收到解决方案:(@hyperboreean:你能在某个地方发布一个zip来演示这个问题吗?有一些数据要测试,可能更容易找出实际问题。我不知道这是否是Python版本的问题,但我得到了TypeError:参数应该是整数或类似于object的字节,而不是'str',这可以通过替换括号中的所有内容来解决d“data.find”与b'\x50\x4b\x05\x06'相同@morp3us,您最终找到解决方案了吗?
# 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')