是否有用于从远程zip检索文件的库? [英] Is there a library for retrieving a file from a remote zip?
问题描述
目标只是在不下载整个内容的情况下检索特定文件,使用所述的HTTP范围方法:
http://www.codeproject.com/KB/cs/remotezip.aspx
The goal is just to retrieve a specific file without downloading the entire contents, using the HTTP range method as described: http://www.codeproject.com/KB/cs/remotezip.aspx
推荐答案
你可以用更少的代码来解决这个问题。基本上,为ZipFile创建足够的类文件对象。所以你最终得到 z = ZipFile(HttpFile(url))
,它会动态下载所需的部分。这样做的好处是你可以编写更少的代码,它不仅仅适用于zip文件。 (事实上,我想知道是否有这样的东西......虽然我找不到它。)
You can solve this a bit more generally with less code. Essentially, create enough of a file-like object for ZipFile to use. So you wind up with z = ZipFile(HttpFile(url))
and it dynamically downloads just the portion needed. The advantage with this is you write less code, and it applies to more than just zip files. (In fact, I wonder if there is something like this already... I'm not finding it though.)
使用相同的想法,你也可以创建一个为HttpFile缓存包装器以避免重复下载。
Using the same idea, you could also create a caching wrapper for HttpFile to avoid repeated downloads.
这里是代码:(注意缺少错误处理)
And here's the code: (note the lack of error-handling)
#!/usr/bin/python
import urllib2
class HttpFile(object):
def __init__(self, url):
self.url = url
self.offset = 0
self._size = -1
def size(self):
if self._size < 0:
f = urllib2.urlopen(self.url)
self._size = int(f.headers["Content-length"])
return self._size
def read(self, count=-1):
req = urllib2.Request(self.url)
if count < 0:
end = self.size() - 1
else:
end = self.offset + count - 1
req.headers['Range'] = "bytes=%s-%s" % (self.offset, end)
f = urllib2.urlopen(req)
data = f.read()
# FIXME: should check that we got the range expected, etc.
chunk = len(data)
if count >= 0:
assert chunk == count
self.offset += chunk
return data
def seek(self, offset, whence=0):
if whence == 0:
self.offset = offset
elif whence == 1:
self.offset += offset
elif whence == 2:
self.offset = self.size() + offset
else:
raise Exception("Invalid whence")
def tell(self):
return self.offset
这篇关于是否有用于从远程zip检索文件的库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!