My code is as simple as below. I just create a xlsx workbook, save it to a file. Then I reopen it from the file, make some changes and try to overwrite it.
Workbook wb = new XSSFWorkbook();
wb.createSheet("test");
wb.write(new FileOutputStream("file.xlsx"));
wb.close();
// works fine until this point
wb = WorkbookFactory.create(new File("file.xlsx"));
wb.createSheet("test2");
// fails with "Unexpected end of ZLIB input stream"
wb.write(new FileOutputStream("file.xlsx"));
// wb.write(new FileOutputStream("file2.xlsx")); // works fine and creates both files
wb.close();
The initial creation of the file goes ok. I tested pausing the code and opened the file in Excel to make sure it's valid.
I can open the recently created file and make changes. The problem happens on overwrite (wb.write
). I get org.apache.poi.ooxml.POIXMLException: java.io.EOFException: Unexpected end of ZLIB input stream
. The file becomes 0 byte.
Strangely, if I do wb.write(new FileOutputStream("file2.xlsx"));
instead of wb.write(new FileOutputStream("file.xlsx"));
everything works fine. Both files file.xlsx
and file2.xlsx
are created and have the changes applied. This was pointed here.
Also, the problem is not about opened streams or such. Because if I execute only the second part of the code after the file already exists and is valid it also fails.