views:

268

answers:

1

Hello,

I have a inputStream of a docx file and I need to get hold of the document.xml which lies inside the docx.

I am using ZipInputStream to read my stream and my code is something like

    ZipInputStream docXFile = new ZipInputStream(fileName);
    ZipEntry zipEntry;
    while ((zipEntry = docXFile.getNextEntry()) != null) {
        if(zipEntry.getName().equals("word/document.xml"))
        {
            System.out.println(" --> zip Entry is "+zipEntry.getName());
        } 
    }

As you can see The output for zipEntry.getName comes as "word/document.xml" at some point. I need to pass this document.xml as a stream and unlike the ZipFile method where you can easily pass this on calling .getInputStream, I am wondering how can I do this docXFile?

Thanks in advance, Meenakshi

@Update: I found the output for this solution:

       ZipInputStream docXFile = new ZipInputStream(fileName);
    ZipEntry zipEntry;
    OutputStream out;

    while ((zipEntry = docXFile.getNextEntry()) != null) {
        if(zipEntry.toString().equals("word/document.xml"))
        {
            System.out.println(" --> zip Entry is "+zipEntry.getName());
            byte[] buffer = new byte[1024 * 4];
            long count = 0;
            int n = 0;
            long size = zipEntry.getSize();
            out = System.out;

            while (-1 != (n = docXFile.read(buffer)) && count < size) {
                out.write(buffer, 0, n);
               count += n;
            }
        }
    }

I am wondering if there is some basic API to convert this output stream to input stream?

+1  A: 

Something like this should work (not tested):

ZipFile zip = new ZipFile(filename)
Enumeration entries = zip.entries();
while ( entries.hasMoreElements()) {
   ZipEntry entry = (ZipEntry)entries.nextElement();

   if ( !entry.getName().equals("word/document.xml")) continue;

   InputStream in = zip.getInputStream(entry);
   handleWordDocument(in);
}

Further you might take a look at some other zip library besides the built in one. AFAIK the build-in one does not support all modern compression levels / encryption and other stuff.

ZeissS