File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing image in a zip file

 
colin shuker
Ranch Hand
Posts: 750
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
colin shuker
Ranch Hand
Posts: 750
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Thanks
 
colin shuker
Ranch Hand
Posts: 750
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
}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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic