First, sorry for english. I wanna know if you can help me to resolve this problem. What I'm trying is to download a zip that I have create from multiple byte[] using Struts2 and stream result.
I use ZipOutputStream and I have managed to create a File from it and read and download it using FileInputStream, but my problem is that I don't wan't to create a File. I just wanna convert the ZipOutputStream into InputStream (for example into ZipIntputStream) and download that ZipInputStream. For doing this I use this code:
public void downloadZip() {
contentType = "application/octet-stream";
filename="myZip.zip";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bytes;
try {
ZipOutputStream zos = new ZipOutputStream(baos);
ZipEntry ze;
bytes = otherClass.getBytes("File1");
ze = new ZipEntry("File1.pdf");
zos.putNextEntry(ze);
zos.write(bytes);
zos.closeEntry();
bytes = otherClass.getBytes("File2");
ze = new ZipEntry("File2.pdf");
zos.putNextEntry(ze);
zos.write(bytes);
zos.closeEntry();
zos.flush();
inputStream = new ZipInputStream(new ByteArrayInputStream(baos.toByteArray()));
zos.close();
}
catch(Exception e){...}
}
My action struts.xml
<action...>
<result name="success" type="stream">
<param name="contentType">${contentType}</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${filename}"</param>
<param name="bufferSize">1024</param>
</result>
</action>
The problem is that the browser shows me a message says to me that the file it's not a valid zip, and its size is 0 bytes.
I hope I explained clearly, thanks a lot in advance.
Edit: As I have commented, finally I get the solution and it's very similar to leonbloy's reply. Besides return the ByteArrayInputStream I should close the ZipOutputStream before create the ByteArrayInputStream. Here's the result code, maybe it can be useful for other people:
...
zos.closeEntry();
zos.close();
inputStream = new ByteArrayInputStream(baos.toByteArray());
}
catch(Exception e){...}
}
Thanks for your help.