// Q: How do I display an image that's contained in a zip file?

// WARNING!  I don't suggest you use this if you don't understanding it.

import java.awt.*;
import java.applet.*;

import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.util.ArrayList;
import java.io.InputStream;

public class ZipImage extends Applet
{
	Image img;

	public void init()
	{
		ZipFile zfile = null;

		try {
			zfile = new ZipFile("test.zip");
		} catch ( Exception e) {}

		// build an ArrayList of the ZipEntries in the ZipFile
		ArrayList<ZipEntry> toc = new ArrayList<ZipEntry>();
		java.util.Enumeration e = zfile.entries();
		while ( e.hasMoreElements() )
			toc.add((ZipEntry)e.nextElement());

		// find the first PNG in the zipfile
		int png;
		for ( png=0; png<toc.size(); png++ )
			if ( toc.get(png).getName().endsWith(".png") )
				break;

		InputStream stream = null;

		try {
			stream = zfile.getInputStream( toc.get(png) );
		} catch (Exception e2) {}

		// decompress image into raw byte array, and make an Image from that
		int nbytes = (int)toc.get(png).getSize();
		int count = 0;
		byte[] imagedata = new byte[nbytes];

		try {
			// attempt to slurp entire file into the array at once
			count = stream.read(imagedata);
			// if ( count != nbytes ) then something went wrong
			stream.close();
		} catch (Exception e3) {}

		img = Toolkit.getDefaultToolkit().createImage(imagedata);
	}


	public void paint( Graphics g )
	{
		g.drawImage(img,100,100,this);
	}
}
