Pyglet拉链

2024-09-29 17:16:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我发现Pyglet有一个类,我可以用它加载zip文件: 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文件. 我得到的文件实际上是纯文本。有什么方法可以转换吗?你知道吗


Tags: 文件orgapihttpdochtmlwwwzip
2条回答

我也试着弄清楚如何从拉链上加载文件。你知道吗

显然,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.

好吧,我想它还没有实施。类所做的唯一事情就是在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'

相关问题 更多 >

    热门问题