seek() Python 中 zip 文件中的文件,而不将其传递到内存 [英] seek() a file within a zip file in Python without passing it to memory

查看:29
本文介绍了seek() Python 中 zip 文件中的文件,而不将其传递到内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让 zip 文件中的文件可以在 Python 中查找而不将其读入内存?

is there anyway to make a file inside a zip file seekable in Python without reading it to memory?

我尝试了明显的程序,但由于文件不可查找而出现错误:

I tried the obvious procedure but I get an error since the file is not seekable:

In [74]: inputZipFile = zipfile.ZipFile("linear_g_LAN2A_F_3keV_1MeV_30_small.zip", 'r')

In [76]: inputCSVFile = inputZipFile.open(inputZipFile.namelist()[0], 'r')   

In [77]: inputCSVFile
Out[77]: <zipfile.ZipExtFile at 0x102f5fad0>

In [78]: inputCSVFile.se
inputCSVFile.seek      inputCSVFile.seekable  

In [78]: inputCSVFile.seek(0)
---------------------------------------------------------------------------
UnsupportedOperation                      Traceback (most recent call last)
<ipython-input-78-f1f9795b3d55> in <module>()
----> 1 inputCSVFile.seek(0)

UnsupportedOperation: seek

推荐答案

没有办法对所有 zip 文件这样做.DEFLATE 是一种流压缩算法,这意味着如果没有解压缩文件的任意部分,就无法解压缩文件的任意部分.它可以可能针对已存储的文件实施,但随后您将处于不利位置,其中某些条目可查找而其他条目不可查找.

There is no way to do so for all zip files. DEFLATE is a stream compression algorithm, which means that there is no way to decompress arbitrary parts of the file without having decompressed everything before it. It could possibly be implemented for files that have been stored, but then you get in the unfavorable position where some entries are seekable and others aren't.

这篇关于seek() Python 中 zip 文件中的文件,而不将其传递到内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆