• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessing image in a zip file

 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic