Page 1 of 1

Problem loading via stream from resources

PostPosted: Thu Sep 08, 2016 7:15 pm
by RichardM
I'm trying to load a docx-File (a simpe test one) as a stream from the resources of my java project.

Code: Select all
InputStream stream = this.getClass().getResourceAsStream("test.docx");
WordprocessingMLPackage a = WordprocessingMLPackage.load(stream);


Unfortunately, this gives me the following exception:

Code: Select all
org.docx4j.openpackaging.exceptions.Docx4JException: Error processing zip file (is it a zip file?)


If is use a FileInputStream loading the same file from a normal file path instead everything works fine.

Code: Select all
FileInputStream fStream = new FileInputStream("C:\\test.docx");
WordprocessingMLPackage a = WordprocessingMLPackage.load(fStream);


Thus, it seems something is broken with loading from the resources. Other people such as https://stackoverflow.com/questions/16894199/docx4j-cant-open-template-file-on-mac-os-x seem to have the same problem, too.

Am I doing something wrong, is that a bug or intended?

Re: Problem loading via stream from resources

PostPosted: Fri Sep 09, 2016 3:19 am
by jason
Tested just now on docx4j 3.3.1; works fine for me.

If the resource is missing, I get:

Code: Select all
Exception in thread "main" org.docx4j.openpackaging.exceptions.Docx4JException: Error reading from the stream
   at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:373)
   at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:345)
   at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:310)
   at org.docx4j.openpackaging.packages.WordprocessingMLPackage.load(WordprocessingMLPackage.java:180)

Caused by: java.io.IOException: Stream closed
   at java.io.BufferedInputStream.getInIfOpen(Unknown Source)
   at java.io.BufferedInputStream.fill(Unknown Source)
   at java.io.BufferedInputStream.read1(Unknown Source)
   at java.io.BufferedInputStream.read(Unknown Source)
   at java.io.FilterInputStream.read(Unknown Source)
   at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:370)
   ... 4 more



The error would be different if the resource was corrupt.

Re: Problem loading via stream from resources

PostPosted: Mon Sep 12, 2016 9:02 pm
by RichardM
After some more investigation I found the problem. We use maven as build tool. Unfortunately, maven alters the docx file when packaging everything as jar. This problem and the solution (excluding from filtering) is described here http://stackoverflow.com/questions/1002 ... clipse-ide . Hope, this will help other people facing the same problem. Topic can be closed.