简体   繁体   English

Python zipfile:中央目录的魔术数字错误

[英]Python zipfile: Bad magic number for central directory

Ok, so basically I'm trying to write simple quick script in python to search xml from *.fla (flash) files.好的,所以基本上我正在尝试在 python 中编写简单的快速脚本,以从 *.fla(flash)文件中搜索 xml。 All I'm doing, is opening *.fla files from project via zipfile.ZipFile, go through all files in this zip archive, and search specific term by regex (dirty and simple).我正在做的就是通过 zipfile.ZipFile、go 以及此 zip 存档中的所有文件从项目中打开 *.fla 文件,并通过正则表达式搜索特定术语(脏且简单)。 This is not the ideal solution for my problem, but this will work for now.这不是解决我的问题的理想解决方案,但现在可以使用。 I'm using CS6, and I know that *.fla files from CS5 and above are basically zip archives with xml (and other files) inside, and I have successfully extracted those files via 7zip on windows.我正在使用 CS6,并且我知道来自 CS5 及更高版本的 *.fla 文件基本上是 zip 档案,其中包含 xml(和其他文件),并且我已经通过 Z0F4137ED1502B50C5D6B08 上的 7zip 成功提取了这些文件。 But somewhy, on half the files from my project, zipfile.ZipFile throws an exception 'Bad magic number for central directory' on creation.但不知何故,在我项目的一半文件中,zipfile.ZipFile 在创建时抛出异常“中央目录的魔数错误”。 The call stack looks like this:调用堆栈如下所示:

  File "fla_search.py", line 92, in try_search_zip                                                                                                            
    with zipfile.ZipFile(fla_path, compression=compression) as zip_view:                                                                                                        
  File "C:\bwn_programs\python\lib\zipfile.py", line 1257, in __init__                                                                                                     
    self._RealGetContents()                                                                                                    
  File "C:\bwn_programs\python\lib\zipfile.py", line 1352, in _RealGetContents                                                                                                
    raise BadZipFile("Bad magic number for central directory")

I also have checked header magic number for the faulty file (just in case), and it seems it is actually corresponds to the actual zip archive:我还检查了错误文件的 header 幻数(以防万一),它似乎实际上对应于实际的 zip 存档:

在此处输入图像描述

在此处输入图像描述

(and yes, all contents of the file successfully opens via 7zip) (是的,文件的所有内容都通过 7zip 成功打开)

So, what could be the problem?那么,可能是什么问题?

You hex dump shows the start of the file and the first 4 bytes are indeed a valid local header signature.您的十六进制转储显示文件的开头,前 4 个字节确实是有效的本地 header 签名。 The problem is the python code is complaining about the central directory header - this is near the end of the file.问题是 python 代码抱怨中央目录 header - 这是文件末尾附近。

Some programs that use zip as their container format do non-standard things.一些使用 zip 作为容器格式的程序会做一些非标准的事情。 That means the the files are not true zip files anymore.这意味着这些文件不再是真正的 zip 文件。 The likes of 7zip & unzip have the smarts to work around some, but not all, of these. 7zip 和 unzip 之类的软件很聪明,可以解决其中的一些问题,但不是全部。

If you have unzip available on your windows setup, try running unzip -t yourfile.fla to test the fla file - that may give more clues about the problem with how the file has been constructed.如果您的 windows 设置上有可用的unzip文件,请尝试运行unzip -t yourfile.fla来测试fla文件 - 这可能会提供有关文件构造问题的更多线索。

Are there any public fla files available that have this problem?是否有任何可用的公共fla文件存在此问题? That would make it easier for us to help root cause the issue.这将使我们更容易帮助解决问题的根本原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM