for tar.gz

Assuming we have a func read_from_s3 that reads from s3…

import tarfile

targz = read_from_s3(bucket, s3fn)
tar = tarfile.open(fileobj=io.BytesIO(targz), mode="r:gz")
blahstream = tar.extractfile('blah-filename')

for zip files

from zipfile import ZipFile

with ZipFile('foo.zip') as zip_archive:
  foo = zip_archive.read('some/file.txt')