Fix Unexpected end of ZLIB input stream using Apache POI

Mi Guoliang

Pretty sure the problem is you overwriting the file. Try to save on a different path. If you still want to overwrite the file, save to something else, delete the original, then rename the file you wrote into place:

    String tempFilename = createTempFile(template);
    String outputFilename = StringUtils.removeEndIgnoreCase(tempFilename, ".temp");
    FileUtils.forceDelete(new File(outputFilename));
    try (
            XWPFDocument document = new XWPFDocument(;
            OutputStream outputStream = FileUtils.openOutputStream(new File(outputFilename))
    ) {
    FileUtils.forceDelete(new File(tempFilename));

A similar issue in Stackoverflow:

If you like my share, you can:

• Follow My Programming Experiences Page on Facebook.

• Follow My Twitter.

• Subscribe to my mail list.