'Python zipfile try except not passing over "zlib.error: Error -3 while decompressing data: invalid distance too far back"
I am working on a python script to unzip a folder of nested zip files. In general, it seems to be working, but when it comes to one of the files I always receive this error:
zlib.error: Error -3 while decompressing data: invalid distance too far back
I initially tried using a try
and except
to pass over this, but that didn't work. I then tried adding into the except clause except zipfile.BadZipFile
after reading various posts on here regarding it, but I still am receiving the same error. Here is how my code looks:
import os
import zipfile
folder_path = [MY FOLDER PATH HERE]
def extract_zip(filepath_in, filepath_out, password):
if is_zip(filepath_in):
print(filepath_in)
with zipfile.ZipFile(filepath_in, 'r') as zip_file:
zip_file.extractall(filepath_out, pwd = bytes(password, 'utf-8'))
error_list = []
zip_count = 1
while zip_count > 0:
filelist = []
for root, dirs, files in os.walk(folder_path):
for file in files:
filelist.append(os.path.join(root,file))
zip_count = 0
for name in filelist:
if name[(len(name) - 4):(len(name))] == ".zip": #if file name ends in .zip
zip_count =+ 1
try:
extract_zip(name, name[0:(len(name) - 4)] , password)
os.remove(name)
except zipfile.BadZipFile as fail:
error_list.append(name), fail
pass
I am unsure of how else to either have the error ignored or if there is some other way to extract the file.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|