Python tar file how to extract file to stream

I am trying to extract a zipped folder, but instead of using .extractall() directly, I want to extract the file to a stream so that I can handle the stream myself. Can this be done using tarfile ? Or have any suggestions?

+4
source share
2 answers

You can get each file from a tar file as a python file object using the .extractfile() method. Scroll tarfile.TarFile() instance to display all entries:

 import tarfile with tarfile.open(path) as tf: for entry in tf: # list each entry one by one fileobj = tf.extractfile(entry) # fileobj is now an open file object. Use `.read()` to get the data. # alternatively, loop over `fileobj` to read it line by line. 
+13
source

I was unable to extractfile while streaming the network tar file, I did something like this:

 from backports.lzma import LZMAFile import tarfile some_streamed_tar = LZMAFile(requests.get('http://some.com/some.tar.xz').content) with tarfile.open(fileobj=some_streamed_tar) as tf: tarfileobj.extractall(path="/tmp", members=None) 

And read them:

 for fn in os.listdir("/tmp"): with open(os.path.join(t, fn)) as f: print(f.read()) 

python 2.7.13

0
source

All Articles