2013-02-24 45 views
0

我發現Pyglet具有類,我可以加載zip文件: http://www.pyglet.org/doc/api/pyglet.resource.ZIPLocation-class.htmlPyglet的ZIPLocation

還有就是我如何使用它:

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的方式使用。我實際上得到了純文本文件。有什麼方法可以轉換它嗎?

回答

0

好吧,我想它仍然沒有實現。該類所做的唯一事情就是在StringIO中返回文件的數據。用純zip文件做這件事更容易。這是我怎麼做的:

# 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

我一直試圖弄清楚如何從ZIP探頭加載文件了。

顯然,ZIPLocation主要用於Pyglet,以找到打開它的ZIP文件。您可以通過將ZIP文件添加到路徑來打開ZIP文件:

pyglet.resource.path.append("./spam.zip") 
pyglet.resource.reindex() 
data = pyglet.resource.file("spam.txt").read()#Imagine spam.txt is inside the zip.