Question

We are trying to modify one of the XML files in 2007 MS Excel. For this, the xlsx file is being unzipped using Java's ZipInputStream and then copied to a new Zip file using ZipOutputStream. The code snippets are as below :

        FileInputStream fis = new FileInputStream("C:\\_132139TRD-GDR_Conversion.xlsx");
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));

        FileOutputStream fos =  new FileOutputStream("C:\\_132139TRD-GDR_Conversion123.xlsx");
        ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));

        while ((entry = zis.getNextEntry()) != null) {

            System.out.println("Processing Entry : " + entry.getName());
            System.out.println("Processing Entry Size : " + entry.getSize());
            System.out.println("Entry Available : " + zis.available());

        if(entry.getName().equals(ZIP_ENTRY_WOOKBOOK_XML)){
          //Process XML
        }

        ZipEntry ze = new ZipEntry(entry.getName());
            zos.putNextEntry(ze);
            if(entry.getName().equals(ZIP_ENTRY_WOOKBOOK_XML)){
                    zos.write(XML.getBytes());
            }else{
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                while ((count = zis.read(data, 0, BUFFER)) != -1) {
                    stream.write(data, 0, count);
                }
                tempBuffer = stream.toByteArray();
                zos.write(tempBuffer);
            }
            zos.closeEntry();               
        }
        zos.close();
        fos.close();
        zis.close();
        fis.close();

In this snippet, after we encounter a JPEG file, the "entry = zis.getNextEntry()" returns null. We are not able to read the next ZipEntry - which leads to an incomplete Excel. The output for a sample Excel file is as below :

Start Date Mon Dec 06 15:33:33 IST 2010
Processing Entry : xl/media/Logo2005_JPM_A_Black_170x50.jpeg
Processing Entry Size : -1
Entry Available : 1
End Date Mon Dec 06 15:34:58 IST 2010

The ZipInputStream gets closed as soon as the JPEG file is copied. (entryEOF and reachEOF variables both turn true and entry is null after copying the image).

Has anybody faced a similar issue ? Is it a bug that reachEOF also turns true after copying the file ?

Midhun.

Was it helpful?

Solution

This is because you read the data for the image from zis instead of from zis.getInputStream(entry).

After the copy loop, all data from zis will have been read, so you get an EOF.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top