Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing an XML inside a zip in-memory

Tags:

java

dom

xml

zip

I have a Zip that contains two files: an XML and a thumbnail. I would like to open the XML file and parse it WITHOUT having to extract on disk.

One of DocumentBuilder's parse method requires an InputStream. Is there a way to get the InputStream of the XML in the Zipped file? I kinda got lost. I'm pretty sure ZipInputStream or ZipFile has something to offer, but I can't figure it out :/

Thank you in advance!

like image 967
pek Avatar asked Mar 20 '11 00:03

pek


People also ask

Can XML files be zipped?

You can use any modern browser to convert XML to ZIP, for example, Google Chrome, Firefox, Opera, Safari.

What is zip XML?

The .zip extension is the most common archive format utilised across the internet for storing a collection of files and directories in a single compressed file.


1 Answers

I believe you're looking for something like this:

FileInputStream fin = new FileInputStream("your.zip");
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
    if (ze.getName().equals("your.xml")) {
        // pass zin to DocumentBuilder
    }
}
like image 76
WhiteFang34 Avatar answered Oct 19 '22 13:10

WhiteFang34