/** * @(#)ZipImageLoader.java 1.0.1 98/12/04 *

* Copyright (C) 1998 David E. Wexler *

* This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. See * license.txt for a full copy of the GNU GPL. *

* This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. *

* You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, * USA. *

* To Contact the author send e-mail to vagabond@netdragon.com or send * snailmail to: 511 Bobcat Ct, Punta Gorda, FL 3398. */ import java.applet.Applet; import java.awt.*; import java.awt.peer.*; import java.io.*; import java.lang.*; import java.net.*; import java.util.*; import java.util.zip.*; /** * ZipImageLoader - Downloads a compressed Zip file and extracts the * information into an Image Array. *

* This class is designed to cut dynamic load time by allowing the code * developer to zip a large number of images and laod them as arrays * in their program. Not only does this save download time by only * requesting one file, the contents can be compressed. *

* @author David Wexler (vagabond@netdragons.com) * @version 1.0.1 * @since Nelzan1.0.1 */ public class ZipImageLoader { // Edit this variable depending on the path of your images private static final String PATH = "/gfx/"; Image[] myImages; MediaTracker tracker; String url; Applet comp; /** * Creates a new Instance of the class, so it can be used later. * * @param url Base URL, where all the Zip files are kept. * @param c Instance of an Applet, for call backs. */ public ZipImageLoader(String url, Applet c) { this.url = url; comp = c; } /** * Downloads a zip file and extracts the contents into an images array. * This function also makes use a MediaTracker to make sure the images * download and uses CRC32 to make sure the file integrity is good. * * @param file The file to download and extract * @return and Image Array with the contents of the Zip File. */ public Image[] getImages(String file) { tracker = new MediaTracker(comp); myImages = new Image[1]; int size = 0; int downloaded = 0; byte buffer[] = new byte[1024]; ComponentPeer cp = comp.getPeer(); try { URL zipUrl = new URL(url + PATH + file); URLConnection conn = zipUrl.openConnection(); conn.connect(); DataInputStream stream = new DataInputStream(new BufferedInputStream(conn.getInputStream())); size = conn.getContentLength(); ZipInputStream zipStream = new ZipInputStream(stream); ZipEntry zipExtract = null; while ((zipExtract = zipStream.getNextEntry()) != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); long crcReq = zipExtract.getCrc(); CRC32 crc = new CRC32(); while (true) { int nRead = zipStream.read(buffer, 0, buffer.length); if (nRead <= 0) break; baos.write(buffer, 0, nRead); crc.update(buffer, 0, nRead); } if (crcReq != -1 && crc.getValue() != crcReq) { System.out.println("CRC Error extracting file " + zipExtract.getName()); } else { if (downloaded > 0) myImages = addImage(myImages, cp.getToolkit().createImage(baos.toByteArray())); else myImages[0] = cp.getToolkit().createImage(baos.toByteArray()); tracker.addImage(myImages[myImages.length-1], myImages.length-1); downloaded += zipExtract.getCompressedSize(); StringTokenizer Token = new StringTokenizer(zipExtract.getName(), ".", false); String name = Token.nextToken(); byte data[] = baos.toByteArray(); comp.showStatus("Extracting files from Zip Archive: " + file); } } stream.close(); zipStream.closeEntry(); } catch (Exception ex) { System.out.println("Exception: " + ex.getMessage()); ex.printStackTrace(); } comp.showStatus("Verifying files from Zip Archive: " + file); try { tracker.waitForAll(); } catch (Exception ex) { ex.printStackTrace(); } comp.showStatus(""); tracker = null; return myImages; } /** * Used to Add an Image to the end of an Array. * * @param ary The old array. * @param im New Image to add. * @return The new array. */ private Image[] addImage(Image[] ary, Image im) { Image[] temp = new Image[ary.length+1]; for (int i=0; i < ary.length; i++) temp[i] = ary[i]; temp[ary.length] = im; System.gc(); return temp; } }