wood burning stoves*
The moose likes I/O and Streams and the fly likes Accessing image in a zip file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Accessing image in a zip file" Watch "Accessing image in a zip file" New topic

Accessing image in a zip file

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744

If I have an image in a folder, I can access it easily using...

But supposing AAA.jpg was inside somezip.zip, then how can I do the same thing as above to get the image?

Thanks for any help
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
One of the other constructors for ImageIcon allows you to make an ImageIcon from a byte[] array. So, can you get bytes from a zip file? From a quick browse of the API, this should work:

"I'm not back." - Bill Harding, Twister
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thats excellent thanks,

I was working from this example...
zip stuff
But you can see it scans the entire zip file and puts all the contents into a hashtable.
I plan to use this in an applet, so obviously I don't want to have to load in every image at once, I rather just load them in as needed.

Hopefully this should work on applet.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi there again,

I've tried code on applet, but it doesn't work.
Essentially I have the JAR file of the applet, and the somezip.zip file containing the images, in same directory on server.

The applet code is...

The code works fine on eclipse on my computer, but on the server, when I run the applet on IE7, the Sun Java Console says:

Exception in thread "AWT-EventQueue-2" java.lang.NullPointerException
at java.awt.Toolkit.createImage(Unknown Source)
at pack.Drawer.loadImage(Main.java:77)

I don't understand, are applets not allowed to access images from inside of zip files, or is the code I'm using only applicable to applications.

Thanks for any input, it is much appreciated
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41626
}catch(Exception exception){}

I'll bet you dollars for donuts that you're ignoring an exception here.

Jim's code does indeed do file I/O (which applets can't do without being signed, and not with files on the server to begin with). I don't think it'll work that way in an applet. The general outline might be something like:

and then work with the methods of ZipInputStream.

Ping & DNS - my free Android networking tools app
subject: Accessing image in a zip file