Title: Zipshow

Author: Pete Shinners (pete at shinners.org)
Submission date: May 21, 2002

Description: Displays any images contained in a .zip archive.

Download: zipshow.py

pygame version required: Any
SDL version required: Any
Python version required: Any

Comments: Many game projects require a very large number of graphics files - sprite images, backgrounds, textures and other such resources. Some developers simply ship their games with the graphics files as-is, while others prefer to group them together in compressed archives. While it won't keep your distribution size down (unless you're using an uncompressed format like .bmp), python's zipfile module makes it easy to ship your graphics resources in one easy-to handle, familiar archive format.

The zipshow script will display each stored file in a zip archive, one at a time - hit a key or click the mouse to page through them, or hit ESC to quit. Place it in a directory with an archive, or feed it the name of a zip file, and watch it do its thing.

Messages: 0

#/usr/bin/env python

Pete Shinners
May 20, 2002

This shows how to load images from a zipfile. The script itself
will take the name of a zip file and display all the image inside
the zip.

You will want to change to code yourself to suit your game,
but this script shows you how to find images inside a zip file
and then load a specific image from the zip.

This example originally was included as an example with earlier
versions of pygame.

import zipfile, sys, glob, pygame
from cStringIO import StringIO
from pygame.locals import *

def showimage(imgsurface, name):
    "show a loaded image onto the screen"
    size = imgsurface.get_size()
    pygame.display.set_caption(name+'  '+`size`)
    screen = pygame.display.set_mode(size)
    screen.blit(imgsurface, (0,0))

    #loop through events until done with this pic
    while 1:
        e = pygame.event.wait()
        if e.type == QUIT:
            return 0
        if e.type in (KEYDOWN, MOUSEBUTTONDOWN):
    return 1

def zipslideshow(zipfilename):
    "loop through all the images in the zipfile"
    zip = zipfile.ZipFile(zipfilename)
    for file in zip.namelist():
        #get data from zipfile
        data = zip.read(file)

        #create stringio for data (a file-like object)
        data_io = StringIO(data)

        #load from a stringio object (ignore error exceptions)
        try: surf = pygame.image.load(data_io, file)
        except: continue

        #show image on the screen
        if not showimage(surf, file):

def main():
    "run the program, handle arguments"
    zipfiles = sys.argv[1:]
    if not zipfiles:
        zipfiles = glob.glob('*.zip') #find any zipfile in this directory
    if not zipfiles:
        raise SystemExit('No ZIP files given, or in current directory')

#run the script if not being imported
if __name__ == '__main__': main()

Main - Repository - Submit - News