import zipfile
import hashlib
import io


def md5sum(zf: zipfile.ZipFile, filepath: str) -> str:
    """
    Given a ZipFile object and relative filepath within the zip archive,
    returns the md5sum of the file

    Code adapted from qiime2/core/util.py
    """
    md5 = hashlib.md5()
    with zf.open(filepath) as fh:
        for chunk in iter(lambda: fh.read(io.DEFAULT_BUFFER_SIZE), b""):
            md5.update(chunk)
    return md5.hexdigest()