Open an archive (tarball or zip) and return the object.

def open_archive(stream, filename):
    """
    Open an archive (tarball or zip) and return the object.
    """
    name, ext = os.path.splitext(os.path.basename(filename))

    def targz_handler(stream):
        return TarGZAdapter(tarfile.open(fileobj=stream, mode='r:gz'))
    cls = [zipfile.ZipFile, targz_handler]['gz' in ext]
    archive = cls(stream)
    return archive