Ousmane MINTE Ousmane MINTE - 6 months ago 21x
Java Question

Generate xlsx file with POI in a zip

I export data in an xlsx files with the POI Api and added they're in a Zip file. When I open the zip I do not have any xlsx files but three directories (docProps, xl and _rels) and 1 file [Content_Types] xml. I think it's the xlsx file's description, but I don't understand why.

Code :

public InputStream exportXlsx(List<MyObject> listeOfObject) throws IOException {

ByteArrayOutputStream excelOutputStreamZip = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(excelOutputStreamZip);

for (MyObject myObject : listeOfObject) {

XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet wsheet = wb.createSheet("mySheet");
XSSFRow row = wsheet.createRow(0);
XSSFCell cell = row.createCell(1);

// Create all sheet and cell....

// Write WB conntent in outputStream

addEntry(zip, myObject.getFileName(), excelOutputStreamZip);

InputStream inputStreamZipByte = new ByteArrayInputStream(
((ByteArrayOutputStream) excelOutputStreamZip).toByteArray());

return inputStreamZipByte;


public void addEntry(OutputStream zip, String filename, ByteArrayOutputStream os) {

byte[] bytes = os.toByteArray();

ZipEntry entry = new ZipEntry(filename);
Date d = new Date();
try {
((ZipOutputStream) zip).putNextEntry(entry);
((ZipOutputStream) zip).write(bytes);
((ZipOutputStream) zip).closeEntry();
} catch (IOException e) {
log.error("Can't read the file !", e);
} catch (ClassCastException cce) {
log.error("Bad format !", cce);


This code is write in a service which is inject in a Struts2 action.

struts.xml :

<action name="*MyAction" class="com.omb.view.action.myAction" method="{1}">
<result name="export" type="stream">
<param name="contentType">application/zip</param>
<param name="inputName">inputStreamZipByte</param>
<param name="contentDisposition">attachment;filename="myZip.zip"</param>
<param name="bufferSize">1024</param>


I found a part of my solution, but now I have another problem :

the problem for the inital post was in the handling of streams. Because I used the same outputStream for the Zip and the Workbook. Solution was created a new ByteArrayOutpuStream for each workbook.

// Write WB conntent in outputStream    
ByteArrayOutputStream wbOutputStream = new ByteArrayOutputStream();  
addEntry(zip, myObject.getFileName(), wbOutputStream);    

But...now the Zip file generated is corrupted...