Load Images off JAR Files

Packing resources (classes, sound files, images) in a JAR file is good way of reducing download time and decreasing distribution size. This tip shows you how to load images off a JAR file.Say you have a JAR file containing some GIF images that your application will use. The following class provides some means of extraction from a JAR file:

 public class JarExtractor { private Hashtable m_sizes=new Hashtable(); private Hashtable m_jarContents=new Hashtable(); private String m_jarFileName; public JarExtractor(String jarFileName) { this.m_jarFileName=jarFileName; initialize(); } public byte[] getImageData(String name) { return (byte[])m_jarContents.get(name); } private void initialize() { ZipFile zf=null; try { // let's get the sizes. zf=new ZipFile(m_jarFileName); Enumeration e=zf.entries(); while (e.hasMoreElements()) { ZipEntry ze=(ZipEntry)e.nextElement(); m_sizes.put(ze.getName(),new Integer((int)ze.getSize())); } zf.close(); zf=null; // put resources into m_jarContents FileInputStream fis=new FileInputStream(m_jarFileName); BufferedInputStream bis=new BufferedInputStream(fis); ZipInputStream zis=new ZipInputStream(bis); ZipEntry ze=null; while ((ze=zis.getNextEntry())!=null) { if (ze.isDirectory()) { continue; } int size=(int)ze.getSize(); if (size==-1) { size=((Integer)m_sizes.get(ze.getName())).intValue(); } byte[] b=new byte[(int)size]; int rb=0; int chunk=0; while (((int)size - rb) > 0) { chunk=zis.read(b,rb,(int)size - rb); if (chunk==-1) { break; } rb+=chunk; } zis.close(); m_jarContents.put(ze.getName(),b); } } catch (NullPointerException e){/*handle exception*/} catch (FileNotFoundException e){/*handle exception*/} catch (IOException e){/*handle exception*/} } 

Now, say, we have a JAR file “images.jar” that contains an image file named “myImage.gif”. To get that image we can do:

 JarExtractor je = new JarExtractor ("Images.jar"); Image anImage = Toolkit.getDefaultToolkit().createImage (je.getImageData ("myImage.gif"); 

This uses getImageData of JarExtractor to extract the raw byte data of the image we want. The same principle can be applied to loading of sound files, class files, etc. from a JAR file.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.