gpt4 book ai didi

python - 使用Python,你如何纯粹在内存中解压?

转载 作者:IT老高 更新时间:2023-10-28 22:25:31 50 4
gpt4 key购买 nike

我在一个无法将任何内容保存到磁盘的环境中工作。我需要能够在不保存到磁盘的情况下提取tar文件并解压缩它们。这似乎失败了:
我已经尝试过了,但它会抛出错误:

# fetch.py
from cStringIO import StringIO
import requests
url = "http://example.com/data.tar.gz"
response = requests.get(url)

# ERROR is thrown here. Error shown below
tar = tarfile.open(mode= "r:gz", fileobj = StringIO(response.content))

# This SHOULD break as tar.extract() saves to disk.
# Can't tell because of error on previous line of code.
data = tar.extract()

正如上面代码块中所描述的,我在错误行上得到了以下跟踪:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "./importers/bestbuy_fetcher.py", line 23, in download_bestbuy_batch
tar = tarfile.open(mode= "r:gz", fileobj = StringIO(response.content))
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/tarfile.py", line 1662, in open
return func(name, filemode, fileobj, **kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/tarfile.py", line 1711, in gzopen
**kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/tarfile.py", line 1689, in taropen
return cls(name, mode, fileobj, **kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/tarfile.py", line 1568, in __init__
self.firstmember = self.next()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/tarfile.py", line 2324, in next
raise ReadError(str(e))
ReadError: invalid header

最佳答案

我怀疑这个错误告诉你tarfile的文件格式是错误的。尝试使用wget提取文件,然后在命令行中取消对其的拖动。
另一个关于如何停止python将文件内容写入磁盘的问题需要更仔细地查看tarfileapi。我认为您需要返回tar文件中每个成员的名称,而不是调用TarFile.extract()。然后,可以使用getnames()获取该成员的内容:

 |  extractfile(self, member)
| Extract a member from the archive as a file object. `member' may be
| a filename or a TarInfo object. If `member' is a regular file, a
| file-like object is returned. If `member' is a link, a file-like
| object is constructed from the link's target. If `member' is none of
| the above, None is returned.
| The file-like object is read-only and provides the following
| methods: read(), readline(), readlines(), seek() and tell()

下面是一个例子:
import tarfile    

# Open tarfile
tar = tarfile.open(mode="r:gz", fileobj = file('foo.tgz'))

# Iterate over every member
for member in tar.getnames():
# Print contents of every file
print tar.extractfile(member).read()

关于python - 使用Python,你如何纯粹在内存中解压?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8858414/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com