解压文件导致“BadZipFile: File is not a zip file" [英] unzipping file results in "BadZipFile: File is not a zip file"

查看:148
本文介绍了解压文件导致“BadZipFile: File is not a zip file"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 zip 文件,它们都可以用 Windows 资源管理器和 7-zip 打开.

I have two zip files, both of them open well with Windows Explorer and 7-zip.

但是,当我使用 Python 的 zipfile 模块 [ zipfile.ZipFile("filex.zip") ] 打开它们时,其中一个被打开但另一个给出错误BadZipfile: File is not a zip file".

However when i open them with Python's zipfile module [ zipfile.ZipFile("filex.zip") ], one of them gets opened but the other one gives error "BadZipfile: File is not a zip file".

我通过使用 7-Zip 打开它并查看其属性(如 7Zip.ZIP)来确保后者是一个有效的 Zip 文件.当我用文本编辑器打开文件时,前两个字符是PK",表明它确实是一个zip文件.

I've made sure that the latter one is a valid Zip File by opening it with 7-Zip and looking at its properties (says 7Zip.ZIP). When I open the file with a text editor, the first two characters are "PK", showing that it is indeed a zip file.

我正在使用 Python 2.5 并且真的不知道如何去做.我已经在 Windows 和 Ubuntu 上都试过了,但在两个平台上都存在问题.

I'm using Python 2.5 and really don't have any clue how to go about for this. I've tried it both with Windows as well as Ubuntu and problem exists on both platforms.

更新: Windows 上 Python 2.5.4 的回溯:

Update: Traceback from Python 2.5.4 on Windows:

Traceback (most recent call last):
File "<module1>", line 5, in <module>
    zipfile.ZipFile("c:/temp/test.zip")
File "C:\Python25\lib\zipfile.py", line 346, in init
    self._GetContents()
File "C:\Python25\lib\zipfile.py", line 366, in _GetContents
    self._RealGetContents()
File "C:\Python25\lib\zipfile.py", line 378, in _RealGetContents
    raise BadZipfile, "File is not a zip file"
BadZipfile: File is not a zip file

基本上当调用 _EndRecData 函数从 End of Central Directory 获取数据时,注释长度校验失败 [endrec[7] == len(comment)].

Basically when the _EndRecData function is called for getting data from End of Central Directory" record, the comment length checkout fails [ endrec[7] == len(comment) ].

_EndRecData函数中locals的值如下:

The values of locals in the _EndRecData function are as following:

 END_BLOCK: 4096,
 comment: '\x00',
 data: '\xd6\xf6\x03\x00\x88,N8?<e\xf0q\xa8\x1cwK\x87\x0c(\x82a\xee\xc61N\'1qN\x0b\x16K-\x9d\xd57w\x0f\xa31n\xf3dN\x9e\xb1s\xffu\xd1\.....', (truncated)
 endrec: ['PK\x05\x06', 0, 0, 4, 4, 268, 199515, 0],
 filesize: 199806L,
 fpin: <open file 'c:/temp/test.zip', mode 'rb' at 0x045D4F98>,
 start: 4073

推荐答案

以 file 命名的文件可能会混淆 python - 尝试将其命名为其他名称.如果它STILL不起作用,试试这个代码:

files named file can confuse python - try naming it something else. if it STILL wont work, try this code:

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("Trancating 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  

这篇关于解压文件导致“BadZipFile: File is not a zip file"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆