我发现Pyglet有一个可以加载压缩文件的类:http://www.pyglet.org/doc/api/pyglet.resource.ZIPLocation-class.html
下面是我使用它的方法:
myzip = zipfile.ZipFile('testzip.zip')
myzip = pyglet.resource.ZIPLocation(myzip, '')
myzip = myzip.open('test.png', mode='rb')但是它返回的是<StringIO.StringIO instance at 0x41ec670>,所以我不能像使用pyglet.resource.image那样使用它。我得到的文件实际上是纯文本的。有什么方法可以转换它吗?
发布于 2013-02-25 13:17:13
好吧,我猜它还没有实现。这个类唯一要做的就是以StringIO格式返回文件数据。使用纯zipfile更容易做到这一点。这就是我是如何做到的:
# That class is necessary, it's explained why in Loader's class comments
class Cleaner(dict):
pass
class Loader:
def __init__(self):
self.sprite = pyglet.resource.image(self.unzip('test.png'))
self.sprite = pyglet.resource.image(self.unzip('test2.png'))
def unzip(self, file):
zip = zipfile.ZipFile('test.zip')
file = open('.buffer', 'wb')
# without 'b' it wont work on windows
file.write(zip.read(file))
file.close()
'''now the tricky part: pyglet save every file with weakref to
dont load save thing more than once, it wouldnt let to load
files from buffer so we need to block it somehow after each
file reading i do that with empty dict class (dont need to import weakref)'''
pyglet.resource._default_loader._cached_images = Cleaner()
return 'data/.buffer'发布于 2013-05-06 15:11:47
我也试着弄清楚如何从ZIPs加载文件。
显然,ZIPLocation主要是用来让Pyglet在你用它打开的拉链上找到自己的路。您可以通过将ZIP文件添加到以下路径来打开它们:
pyglet.resource.path.append("./spam.zip")
pyglet.resource.reindex()
data = pyglet.resource.file("spam.txt").read()#Imagine spam.txt is inside the zip.https://stackoverflow.com/questions/15048425
复制相似问题