how to read an image from a zipfile

hello, i have a zip file that contains images, which i want to load so that they exist as BufferedImages.

i already got so far, that i have a ZipEntry and the corresponding BufferedReader but how can i convert the stream from the BufferedReader into a suitable format that is accepted by the ImageIO class?

thanks!

My first thought is to rename it to .jar, add it to your classpath and load the images as you would normally do.

erikd, thanks for your help. that would be an option. maybe with loadRessourceAsStream().

but still, i would very much like to know how it is done by using the java zip-api.

thanks!

Something like this, I think:


        ZipFile zip = new ZipFile("images.zip");
        ZipEntry ze = zip.getEntry("myImage.png");
        InputStream zis = zip.getInputStream(ze);
        BufferedImage bi = ImageIO.read(zis);

thanks a lot!
that worked!!!

My first thought is to rename it to .jar, add it to your classpath[…]

You can also add Zips (and directories) to the cp.