Python 中 ZipFile 模块出现错误的幻数错误

发布于 2024-12-09 10:21:36 字数 1131 浏览 0 评论 0原文

我在 Windows 7(64 位)上使用 Python 2.7。 当我尝试使用 ZipFile 模块解压缩 zip 文件时,出现以下错误:-

Traceback (most recent call last):
  File "unzip.py", line 8, in <module>
    z.extract(name)
  File "C:\Python27\lib\zipfile.py", line 950, in extract
    return self._extract_member(member, path, pwd)
  File "C:\Python27\lib\zipfile.py", line 993, in _extract_member
    source = self.open(member, pwd=pwd)
  File "C:\Python27\lib\zipfile.py", line 897, in open
    raise BadZipfile, "Bad magic number for file header"
zipfile.BadZipfile: Bad magic number for file header

WinRAR 可以正常解压我试图解压的文件。 这是我用来从 myzip.zip 中提取文件的代码。

from zipfile import ZipFile
z = ZipFile('myzip.zip')   //myzip.zip contains just one file, a password protected pdf        
for name in z.namelist():
    z.extract(name)

此代码对于我使用 WinRAR 创建的许多其他 zip 文件运行良好,但 myzip.zip

我尝试注释以下内容Python27\Lib\zipfile.py 中的行:-

if fheader[0:4] != stringFileHeader:
   raise BadZipfile, "Bad magic number for file header"

但这并没有真正帮助。运行我的代码时,我的 shell 上会得到一些转储。

I am using Python 2.7 on Windows 7 (64 bit).
When I try to unzip a zip file with ZipFile module I get the following error:-

Traceback (most recent call last):
  File "unzip.py", line 8, in <module>
    z.extract(name)
  File "C:\Python27\lib\zipfile.py", line 950, in extract
    return self._extract_member(member, path, pwd)
  File "C:\Python27\lib\zipfile.py", line 993, in _extract_member
    source = self.open(member, pwd=pwd)
  File "C:\Python27\lib\zipfile.py", line 897, in open
    raise BadZipfile, "Bad magic number for file header"
zipfile.BadZipfile: Bad magic number for file header

WinRAR could extract the file I am trying to extract just fine.
Here is the code I used to extract files from myzip.zip

from zipfile import ZipFile
z = ZipFile('myzip.zip')   //myzip.zip contains just one file, a password protected pdf        
for name in z.namelist():
    z.extract(name)

This code is working fine for many other zip files I created using WinRAR but myzip.zip

I tried commenting the following lines in Python27\Lib\zipfile.py:-

if fheader[0:4] != stringFileHeader:
   raise BadZipfile, "Bad magic number for file header"

But this didn't really help. Running my code with this in effect, I get some dump on my shell.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖 2024-12-16 10:21:36

正确的 ZIP 文件开头始终为“\x50\x4B\x03\x04”。您可以使用以下代码测试文件是否真的是 ZIP 文件:

with open('/path/to/file', 'rb') as MyZip:
  print(MyZip.read(4))

它将打印文件头以便您检查。

更新
奇怪的是,testzip() 和所有其他函数都运行良好。你尝试过这样的代码吗?

with zipfile.GzipFile('/path/to/file') as Zip:
  for ZipMember in Zip.infolist():
    Zip.extract(ZipMember, path='/dir/where/to/extract', pwd='your-password')

Correct ZIP files always have "\x50\x4B\x03\x04" in the beginning. You can test whether file is really ZIP file with this code:

with open('/path/to/file', 'rb') as MyZip:
  print(MyZip.read(4))

It will print header of file so you can check.

UPDATE
Strange, testzip() and all other functions work good. Had you tried such code?

with zipfile.GzipFile('/path/to/file') as Zip:
  for ZipMember in Zip.infolist():
    Zip.extract(ZipMember, path='/dir/where/to/extract', pwd='your-password')
等待我真够勒 2024-12-16 10:21:36

确保您确实打开的是 ZIP 文件,而不是扩展名为 .zip 的 RAR 文件。正确的 zip 文件有一个标头,但在本例中未找到该标头。

zipfile 模块只能打开 zip 文件。 WinRAR 还可以打开其他格式,它可能会忽略文件名而只查看文件本身。

Make sure you are really opening a ZIP file, not for example a RAR file named with a .zip extension. Proper zip files have a header, which was not found in this case.

The zipfile module can only open zip files. WinRAR can also open other formats, and it likely ignores the filename and only looks at the file itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文