Seite 1 von 1

Zip Datei Entpacken mit Passwort

Verfasst: Samstag 28. Mai 2022, 10:56
von MatthiDbr
Hello, ich hab ein Problem in meinem Phyton Script. Ohne Passwort klappt alles aber jetzt will ich meine ZipFile Entpacken mit Passwort.

Code: Alles auswählen

import imp
import itertools
import pwd
import string
import sys
import os
from zipfile import ZipFile
import zipfile
from click import password_option
from numpy import extract

with ZipFile("abc.zip", "r") as ZF:
    ZF.extractall("zip-file", pwd = bytes("abc", "utf-8"))
Der Zip Datei Namen ist abc.zip und das Passwort abc.

Der Terminal code ist:
matthias@iMac-von-Matthias PhytonLearning % /usr/local/bin/python3 "/Volumes/Mac/Coding/PhytonLearning/ZIP.py"
/Volumes/Mac/Coding/PhytonLearning/ZIP cracker.py:1: DeprecationWarning: the imp module is deprecated in favour of importlib and slated for removal in Python 3.12; see the module's documentation for alternative uses
import imp
Traceback (most recent call last):
File "/Volumes/Mac/Coding/PhytonLearning/ZIP.py", line 16, in <module>
ZF.extractall("zip-file", pwd = bytes("abc", "utf-8"))
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/zipfile.py", line 1643, in extractall
self._extract_member(zipinfo, path, pwd)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/zipfile.py", line 1696, in _extract_member
with self.open(member, pwd=pwd) as source, \
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/zipfile.py", line 1569, in open
return ZipExtFile(zef_file, mode, zinfo, pwd, True)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/zipfile.py", line 800, in __init__
self._decompressor = _get_decompressor(self._compress_type)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/zipfile.py", line 699, in _get_decompressor
_check_compression(compress_type)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/zipfile.py", line 679, in _check_compression
raise NotImplementedError("That compression method is not supported")
NotImplementedError: That compression method is not supported
matthias@iMac-von-Matthias PhytonLearning %

Re: Zip Datei Entpacken mit Passwort

Verfasst: Samstag 28. Mai 2022, 11:01
von Sirius3
Die Fehlermeldung ist eindeutig: That compression method is not supported
Das Python zipfile-Modul unterstützt nicht alle Zip-Varianten.

Re: Zip Datei Entpacken mit Passwort

Verfasst: Samstag 28. Mai 2022, 12:48
von __blackjack__
Und bei Verschlüsselungen unterstützt das Modul nur eine sehr alte und nicht wirklich sichere Verschlüsselungsmethode. Und kann auch nur die entschlüsseln. Also nicht beispielsweise AES-verschlüsselte ZIP-Dateien.

Anmerkungen zum Quelltext: Da wird extrem viel importiert was gar nicht verwendet wird in den beiden Zeilen. Bei `imp` sollte man zudem die Ausgabe beachten: das Modul ist veraltet und sollte nicht mehr verwendet werden. Und bei einigen Sachen fragt man sich so ein bisschen ob da einfach des Namens wegen importiert wurde, wie `pwd` und `numpy.extract`. Und `zipfile` als Modul wird importiert und nicht verwendet, weil in einer weiteren Zeile `ZipFile` aus dem Modul explizit importiert wird. Da bleibt am Ende nur eine von den 10 Importzeilen übrig.

Namen werden in Python klein_mit_unterstrichen geschrieben. Ausnahmen sind Konstanten (KOMPLETT_GROSS) und Klassen (PascalCase). `ZF` ist keine Konstante. `zf` ist aber auch kein guter Name, weil Namen keine kryptischen Abkürzungen enthalten, oder gar nur daraus bestehen sollten.

``bytes("abc", "utf-8")`` hätte man kürzer als ``b"abc"`` schreiben können. Und `bytes()`/`str()` mit einer Kodierung als zweites Argument ist eher ungewöhnlich. Der Leser ist da `encode()`/`decode()` eher gewohnt. Also ``"abc".encode("utf-8")``.

Code: Alles auswählen

#!/usr/bin/env python3
from zipfile import ZipFile


def main():
    with ZipFile("abc.zip", "r") as zip_file:
        zip_file.extractall("zip-file", pwd=b"abc")


if __name__ == "__main__":
    main()