简体   繁体   English

Python ZipFile 模块缓慢提取受密码保护的 zip

[英]Python ZipFile module extracts password protected zips slowly

i am trying to write a python-script, which should extract a zip file:我正在尝试编写一个 python 脚本,它应该提取一个 zip 文件:

Board: Beagle-Bone black ~ 1GHz Arm-Cortex-a8 , debian wheezy Zipfile: /home/milo/my.zip, ~ 8 MB板:Beagle-Bone black ~ 1GHz Arm-Cortex-a8 ,debian wheezy Zipfile: /home/milo/my.zip, ~ 8 MB

>>> from zipfile import ZipFile
>>> zip = ZipFile("/home/milo/my.zip")
>>> zip.extractall(pwd="tst")

other solutions with opening and reading-> writing the zipfile and extracting even particular file have the same effect.打开和读取的其他解决方案-> 写入 zipfile 并提取特定文件也具有相同的效果。 extracting take about 3-4 minutes.提取大约需要3-4分钟。

Extracting the same file with just using unzip-tool takes less than 2 seconds.仅使用 unzip-tool 提取相同的文件只需不到 2 秒。

Does anyone know what is wonrg with my code, or even with python zipfile lib??有谁知道我的代码是什么,甚至是 python zipfile lib?

Thanks Ajava谢谢阿爪哇

This seems to be a documented issue with the ZipFile module in Python 2.7.这似乎是 Python 2.7 中 ZipFile 模块的一个记录问题。 If you look at the documentation for ZipFile , it clearly mentions:如果您查看ZipFile 的文档,它清楚地提到:

Decryption is extremely slow as it is implemented in native Python rather than C.解密非常慢,因为它是在本机 Python 而不是 C 中实现的。

If you need faster performance, you can either invoke an an external program (like unzip or 7zip) from your code, or make sure the zip files you are working with are not password protected.如果您需要更快的性能,您可以从您的代码中调用一个外部程序(如 unzip 或 7zip),或者确保您使用的 zip 文件没有密码保护。

Copy from my answer https://stackoverflow.com/a/72513075/10860732从我的答案复制https://stackoverflow.com/a/72513075/10860732

It's quite stupid that Python doesn't implement zip decryption in pure c. Python没有在纯c中实现zip解密,这很愚蠢。

So I make it in cython, which is 17 times faster.所以我用 cython 制作它,它快了 17 倍。

Just get the dezip.pyx and setup.py from this gist.只需从此 gist 中获取 dezip.pyx 和 setup.py。

https://gist.github.com/zylo117/cb2794c84b459eba301df7b82ddbc1ec https://gist.github.com/zylo117/cb2794c84b459eba301df7b82ddbc1ec

And install cython and build a cython library并安装 cython 并构建 cython 库

pip3 install cython
python3 setup.py build_ext --inplace

Then run the original script with two more lines.然后再运行两行原始脚本。

import zipfile

# add these two lines
from dezip import _ZipDecrypter_C
setattr(zipfile, '_ZipDecrypter', _ZipDecrypter_C)

z = zipfile.ZipFile('./test.zip', 'r')
z.extractall('/tmp/123', None, b'password')

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

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