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

hyp*_*ean 8 python zipfile

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

- zipfile.is_zipfile(my_file)总是返回False,即使UNIX命令解压缩处理它也没问题.此外,在尝试时,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")

Uri*_*hen 6

在我的文件上运行相同的问题,并能够解决它.我不确定它们是如何生成的,就像上面的例子一样.它们最终都被尾随数据忽略了Windows 7z和失败的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  
Run Code Online (Sandbox Code Playgroud)

  • 我不知道这是否是Python版本问题,但我得到了 TypeError: argument should be integer or bytes-like object, not 'str' 这可以通过用 b'\x50 替换“data.find”后面括号中的所有内容来解决\x4b\x05\x06' (2认同)

Tom*_*ych 1

你说使用less它显示的文件。你是这个意思吗?

less my_file
Run Code Online (Sandbox Code Playgroud)

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

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

看来您(或在 iSeries 计算机上创建 zip 文件的任何人)可以使用 关闭此功能ARCHTEXT(*NONE),或使用ARCHTEXT(*CLEAR)将其从现有 zip 文件中删除。