import logging
import zipfile
import tarfile


def get_archive_member_names(archive_file_name, archive_file_type):
    """
    Gets all of the file names/members of the archive.
    :param archive_file_name: the file name/path of the archive
    :param archive_file_type: the file extention type: 'tar' or 'zip'
    :return: a list of the names/members in the archive on success, None if failure to open/read archive
    :rtype: bool
    """

    logging.info("Checking archive... this may take a moment for large files.")

    try:

        if archive_file_type == 'zip':

            archive = zipfile.ZipFile(archive_file_name, 'r', allowZip64=True)
            return archive.namelist()

        elif archive_file_type == 'tar':

            archive = tarfile.open(archive_file_name, mode='r')
            archive_members = archive.getmembers()
            archive_name_list = []

            for item in archive_members:
                if item.type == tarfile.DIRTYPE:

                    #append a '/' to the end of the directory name to match zip output formatting
                    archive_name_list.append(item.name + '/')

                else:
                    archive_name_list.append(item.name)

            return archive_name_list

    except:

        # expected errors: (zipfile.BadZipFile, tarfile.ReadError), but catch all of them anyways
        logging.error("Error thrown attempting to read/open the archive. Please use 'zip' or 'tar' to create your archive.")

    return None