简体   繁体   English

python tar文件如何将文件提取到流中

[英]python tar file how to extract file into stream

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. 我试图提取一个压缩文件夹,但不是直接使用.extractall() ,我想将文件解压缩为流,以便我自己处理流。 Is it possible to do it using tarfile ? 是否可以使用tarfile来做到这一点? Or is there any suggestions? 或者有什么建议吗?

You can obtain each file from a tar file as a python file object using the .extractfile() method. 您可以使用.extractfile()方法从tar文件中获取每个文件作为python file对象。 Loop over the tarfile.TarFile() instance to list all entries: 循环遍历tarfile.TarFile()实例以列出所有条目:

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.

I was unable to extractfile while network streaming a tar file, I did something like this instead: 我无法在网络流式传输tar文件时extractfile文件,我做了类似的事情:

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 python 2.7.13

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM