python tar file how to extract file into stream

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

I am trying to extract a zipped folder but instead of directly using .extractall(), I want to extract the file into stream so that I can handle the stream myself. Is it possible to do it using tarfile? Or is there any suggestions?

回答1:

You can obtain each file from a tar file as a python file object using the .extractfile() method. Loop over the tarfile.TarFile() instance to list 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. 


回答2:

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

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 to read them:

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

python 2.7.13



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!