'Extract the uploaded capsule archive into a temporary location for inspection, validation
and potential import.'

def extract_capsule_files(self, **kwd):

    return_dict = {}
    tar_archive = kwd.get('tar_archive', None)
    capsule_file_name = kwd.get('capsule_file_name', None)
    if ((tar_archive is not None) and (capsule_file_name is not None)):
        return_dict.update(kwd)
        extract_directory_path = tempfile.mkdtemp(prefix='tmp-capsule-ecf')
        if capsule_file_name.endswith('.tar.gz'):
            extract_directory_name = capsule_file_name.replace('.tar.gz', '')
        elif capsule_file_name.endswith('.tar'):
            extract_directory_name = capsule_file_name.replace('.tar', '')
        else:
            extract_directory_name = capsule_file_name
        file_path = os.path.join(extract_directory_path, extract_directory_name)
        return_dict['encoded_file_path'] = encoding_util.tool_shed_encode(file_path)
        tar_archive.extractall(path=file_path)
        try:
            tar_archive.close()
        except Exception:
            log.exception('Cannot    close    tar_archive')
        del return_dict['tar_archive']
    return return_dict
