website-pictures2/src/pictures/add.py

29 lines
947 B
Python

# Add function for archive
from pictures._util import check_valid_archive, get_already_existing_files
from zipfile import ZipFile
from os.path import join
def add(archive_path: str, files_to_add: [str],
basepath: str = "",
error_on_duplicate=False,
overwrite_duplicate=False):
if not check_valid_archive(archive_path):
raise ValueError("Archive is not valid")
if overwrite_duplicate:
raise NotImplementedError()
if error_on_duplicate and len(
e := get_already_existing_files(archive_path, files_to_add)) > 0:
raise Exception(f"Erroring on duplicate files: {e}")
with ZipFile(archive_path, 'a') as zip:
for file in files_to_add:
basepath_corrected_path = join(basepath, file)
with (
zip.open(basepath_corrected_path, 'w') as zf,
open(file, 'rb') as of
):
zf.write(of.read())