首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pyglet的ZIPLocation

Pyglet的ZIPLocation
EN

Stack Overflow用户
提问于 2013-02-24 11:46:16
回答 2查看 149关注 0票数 0

我发现Pyglet有一个可以加载压缩文件的类:http://www.pyglet.org/doc/api/pyglet.resource.ZIPLocation-class.html

下面是我使用它的方法:

代码语言:javascript
复制
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那样使用它。我得到的文件实际上是纯文本的。有什么方法可以转换它吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-25 13:17:13

好吧,我猜它还没有实现。这个类唯一要做的就是以StringIO格式返回文件数据。使用纯zipfile更容易做到这一点。这就是我是如何做到的:

代码语言:javascript
复制
# 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'
票数 0
EN

Stack Overflow用户

发布于 2013-05-06 15:11:47

我也试着弄清楚如何从ZIPs加载文件。

显然,ZIPLocation主要是用来让Pyglet在你用它打开的拉链上找到自己的路。您可以通过将ZIP文件添加到以下路径来打开它们:

代码语言:javascript
复制
pyglet.resource.path.append("./spam.zip")
pyglet.resource.reindex()
data = pyglet.resource.file("spam.txt").read()#Imagine spam.txt is inside the zip.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15048425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档