Bug 61575 - OpenXML4JRuntimeException when closing OPCPackage
Summary: OpenXML4JRuntimeException when closing OPCPackage
Alias: None
Product: POI
Classification: Unclassified
Component: OPC (show other bugs)
Version: 3.16-FINAL
Hardware: PC Linux
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2017-09-29 12:21 UTC by azad
Modified: 2017-10-04 18:40 UTC (History)
1 user (show)

Source and example files (14.99 KB, application/zip)
2017-09-29 12:21 UTC, azad

Note You need to log in before you can comment on or make changes to this bug.
Description azad 2017-09-29 12:21:55 UTC
Created attachment 35388 [details]
Source and example files

The docx file contains an image causing this exception. It does not happen for any other image in my actual document. Removing the graphic will work just fine with the attached test code.

Error Message:

rg.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to save: an error occurs while saving the package : The part /word/media/image1.wmf failed to be saved in the stream with marshaller org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller@4b85612c
	at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:595)
	at org.apache.poi.openxml4j.opc.OPCPackage.save(OPCPackage.java:1543)
	at org.apache.poi.openxml4j.opc.OPCPackage.save(OPCPackage.java:1526)
	at org.apache.poi.openxml4j.opc.ZipPackage.closeImpl(ZipPackage.java:450)
	at org.apache.poi.openxml4j.opc.OPCPackage.close(OPCPackage.java:479)
	at com.sovelia.office.DocxMetadataWriter.setDocxMetadata(DocxMetadataWriter.java:29)
	at com.sovelia.office.DocxMetadataWriter.main(DocxMetadataWriter.java:12)
Caused by: org.apache.poi.openxml4j.exceptions.OpenXML4JException: The part /word/media/image1.wmf failed to be saved in the stream with marshaller org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller@4b85612c
	at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:586)
	... 6 more
Comment 1 azad 2017-09-29 12:27:47 UTC
However, after the exception thrown and the source docx file is corrupted.
Comment 2 azad 2017-09-29 12:52:24 UTC
different way to write coreproperties also provided exception a bit differently. Use same test file in attachment. First time it is successfully read/writes and second time gives the exception.

try {
            InputStream fis = new FileInputStream(fileName);
            XWPFDocument doc = new XWPFDocument(fis);
            CoreProperties cp = doc.getProperties().getCoreProperties();
            cp.setCreator("Test Creator");
            OutputStream fos = new FileOutputStream(fileName);
        }catch(Exception e){e.printStackTrace();}


java.io.IOException: Failed to read zip entry source
	at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:103)
	at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:324)
	at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
	at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:116)
	at com.sovelia.office.Test.testDocxReadWrite(Test.java:127)
	at com.sovelia.office.Test.main(Test.java:31)
Caused by: java.io.IOException: Zip bomb detected! The file would exceed the max. ratio of compressed file size to the size of the expanded data. This may indicate that the file is used to inflate memory usage and thus could pose a security risk. You can adjust this limit via ZipSecureFile.setMinInflateRatio() if you need to work with files which exceed this limit. Counter: 1326240, cis.counter: 13253, ratio: 0.009992912293400893Limits: MIN_INFLATE_RATIO: 0.01
	at org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream.advance(ZipSecureFile.java:266)
	at org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream.read(ZipSecureFile.java:221)
	at java.io.FilterInputStream.read(FilterInputStream.java:107)
	at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource$FakeZipEntry.<init>(ZipInputStreamZipEntrySource.java:132)
	at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:56)
	at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:100)
	... 5 more
Comment 3 PJ Fanning 2017-09-29 16:24:19 UTC
Java 9 is just out and it looks we have a few issues (eg https://bz.apache.org/bugzilla/show_bug.cgi?id=61564)

There is a plan to issue a 3.17.1 patch to fix issues when Java 9 is used.
Comment 4 PJ Fanning 2017-09-29 16:28:44 UTC
Apologies for the confusion. I put the Java 9 comment on the wrong issue.
Comment 5 PJ Fanning 2017-09-29 16:33:05 UTC
What happens when you adjust ZipSecureFile.setMinInflateRatio() as the error message suggests?
Comment 6 azad 2017-10-02 12:32:23 UTC
(In reply to PJ Fanning from comment #5)
> What happens when you adjust ZipSecureFile.setMinInflateRatio() as the error
> message suggests?

Hi Fanning,
Thanks, it works with the ZipSecureFile.setMinInflateRatio().

However, just to mention that my first example did not provide this proper error message about Minimum Inflate Ratio.

Comment 7 Dominik Stadler 2017-10-04 18:40:14 UTC
The exception is thrown in your original case as well, but sent as log-line to the configured POILogger internally. 

The default logger is not sending any output to keep the lib from spamming stdout, but you can easily define a different one, e.g. by specifying -Dorg.apache.poi.util.POILogger=org.apache.poi.util.SystemOutLogger