Load Images off JAR Files

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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes