import logging from os import makedirs, path from pathlib import Path from typing import List, Union from zipfile import ZipFile import aiofiles logger = logging.getLogger(__name__) USERS_WITH_CONTEXT: List[int] = [] async def extract_and_save(handle: ZipFile, filename: str, destpath: Union[str, Path]): """Extract and save file from archive ### Args: * handle (`ZipFile`): ZipFile handler * filename (`str`): File base name * path (`Union[str, Path]`): Path where to store """ data = handle.read(filename) filepath = path.join(str(destpath), filename) try: makedirs(path.dirname(filepath), exist_ok=True) async with aiofiles.open(filepath, "wb") as fd: await fd.write(data) logger.debug("Unzipped %s", filename) except IsADirectoryError: makedirs(filepath, exist_ok=True) except FileNotFoundError: pass return