Python - Extrahieren von Dateien aus einer großen (6 GB +) ZIP-Datei
Ich habe einPython
Skript, in dem ich den Inhalt einer ZIP-Datei extrahieren muss. Die ZIP-Datei ist jedoch größer als 6 GB.
Es gibt viele Informationen überzlib
undzipfile
Ich kann jedoch keinen einzigen Ansatz finden, der in meinem Fall funktioniert. Ich habe den Code:
with zipfile.ZipFile(fname, "r") as z:
try:
log.info("Extracting %s " %fname)
head, tail = os.path.split(fname)
z.extractall(folder + "/" + tail)
except zipfile.BadZipfile:
log.error("Bad Zip file")
except zipfile.LargeZipFile:
log.error("Zip file requires ZIP64 functionality but that has not been enabled (i.e., too large)")
except zipfile.error:
log.error("Error decompressing ZIP file")
Ich weiß, dass ich das einstellen mussallowZip64
zutrue
aber ich bin mir nicht sicher, wie ich das machen soll. Doch so wie es ist, ist dasLargeZipFile
Ausnahme wird nicht geworfen, sondern dieBadZipFile
Ausnahme ist. Ich habe keine Idee warum.
Ist dies auch der beste Ansatz, um ein 6-GB-Zip-Archiv zu extrahieren?
Update: Ändern derBadZipfile
Ausnahme hiervon:
except zipfile.BadZipfile as inst:
log.error("Bad Zip file")
print type(inst) # the exception instance
print inst.args # arguments stored in .args
print inst
zeigt an:
<class 'zipfile.BadZipfile'>
('Bad magic number for file header',)
Update Nr. 2:
Das vollständige Traceback wird angezeigt
BadZipfile Traceback (most recent call last)
<ipython-input-1-8d34a9f58f6a> in <module>()
6 for member in z.infolist():
7 print member.filename[-70:],
----> 8 f = z.open(member, 'r')
9 size = 0
10 while True:
/Users/brspurri/anaconda/python.app/Contents/lib/python2.7/zipfile.pyc in open(self, name, mode, pwd)
965 fheader = struct.unpack(structFileHeader, fheader)
966 if fheader[_FH_SIGNATURE] != stringFileHeader:
--> 967 raise BadZipfile("Bad magic number for file header")
968
969 fname = zef_file.read(fheader[_FH_FILENAME_LENGTH])
BadZipfile: Bad magic number for file header
Code ausführen:
import sys
import zipfile
with open(zip_filename, 'rb') as zf:
z = zipfile.ZipFile(zf, allowZip64=True)
z.testzip()
doesn't output anything.