How to unzip an archive without overwriting existing files?
The ZipFile.extractall
function is extracting a ZIP file but also overwrite existing file.
So, I wrote my own function:
import os
import zipfile
def unzip(src_path, dst_dir, pwd=None):
with zipfile.ZipFile(src_path) as zf:
members = zf.namelist()
for member in members:
arch_info = zf.getinfo(member)
arch_name = arch_info.filename.replace('/', os.path.sep)
dst_path = os.path.join(dst_dir, arch_name)
dst_path = os.path.normpath(dst_path)
if not os.path.exists(dst_path):
zf.extract(arch_info, dst_dir, pwd)
But, a full implementation could require to re-implement the extract
method.
Is there any way to unzip an archive ignoring the existing files? An equivalent of unzip -n arch.zip
?