It was just necessary to do the same yesterday.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(baos);
.... populate ZipOutputStream
String filename = "out.zip";
response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
response.setContentType("application/zip");
try{
response.getOutputStream().write(baos.toByteArray());
response.flushBuffer();
}
catch (IOException e){
e.printStackTrace();
}
finally{
baos.close();
}
Note. I use the ByteArrayOutputStream and toByteArray wrappers, but you could simply write any other type of Outputstream directly in response using the standard InputStream.read () OutputStream.write () loop.
, , , , ByteArrayOutputStream :