def create_archive(
    directory_path: str,
    filter_func: Callable[[str], bool] = None,
    output_file: Optional[str] = None,
) -> Optional[bytes]:
    """Create tar.gz archive in-memory and return the archive contents as
    a bytes object.

    Args:
        directory_path (str): Directory to create archive of.
        filter_func (_type_, optional): A function which, being passed a filename,
                                        returns whether to include it or not.
                                        Defaults to None (include all).
        output_file (str): Output filename to write the archive to. Usually it ends on .tar.gz.
                           If set to None ( default), the archive will not be written to
                           file but returned as a bytes object.

    Returns:
        Optional[bytes]: Archive contents as a bytes object if output_file was not None
    """
    # Archive files in a directory.

    # Create an in-memory bytes buffer
    if output_file is None:
        buffer = BytesIO()
    else:
        buffer = FileIO(output_file, mode="w+")

    # Determine the appropriate compression mode
    compression_mode = None
    compression_mode = "w:gz"

    # Create a new archive file
    with tarfile.open(fileobj=buffer, mode=compression_mode) as archive:
        # Walk through the directory tree and add each file to the archive
        for root, _, files in os.walk(directory_path):
            for _file in files:
                # Check if the file should be included based on the filter function
                if filter_func is not None:
                    if not filter_func(_file):
                        continue

                # Calculate the relative path of the file
                relative_path = os.path.relpath(
                    os.path.join(root, _file), directory_path
                )

                # Add the file to the archive with the relative path
                archive.add(os.path.join(root, _file), arcname=relative_path)

    # Get the bytes from the buffer
    if output_file is not None:
        buffer.close()
        return None
    buffer.seek(0)
    compressed_bytes = buffer.read()

    return compressed_bytes
Create tar.gz archive in-memory and return the archive contents as
a bytes object.

Args:
directory_path (str): Directory to create archive of.
filter_func (_type_, optional): A function which, being passed a filename,
returns whether to include it or not.
Defaults to None (include all).
output_file (str): Output filename to write the archive to. Usually it ends on .tar.gz.
If set to None ( default), the archive will not be written to
file but returned as a bytes object.

Returns:
Optional[bytes]: Archive contents as a bytes object if output_file was not None
python/aitemplate/utils/io.py