I'm generating a ZIP file that contains a bunch of autogenerated XML files. Recently requirements changed and now i must generate several times that ZIP (with variations on XML data) and serve them directly to client without using actual files in server. This is what im doing:
// [... servlet handling ... ]
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=cierresZ_a_tangonet" + java.time.LocalDate.now() + ".zip");
// stream straight to client
ServletOutputStream out = response.getOutputStream();
ZipOutputStream zipped_out = new ZipOutputStream(out);
for( each data block from db ){
//CREATION AND PROCESSING OF XML FILES AS ZIP ENTRIES
byte[] xmlBinData = xmlData.toString().getBytes();
zipped_out.write(xmlBinData, 0, xmlBinData.length);
zipped_out.flush();
}
zipped_out.finish();
out.close();
}
Tried to do this but gives me errors:
// [... servlet handling ... ]
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=cierresZ_a_tangonet" + java.time.LocalDate.now() + ".zip");
// stream straight to client
ServletOutputStream out = response.getOutputStream();
for( each zip needed ){
ZipOutputStream zipped_out = new ZipOutputStream(out);
for( each data block from db ){
//CREATION AND PROCESSING OF XML FILES AS ZIP ENTRIES
byte[] xmlBinData = xmlData.toString().getBytes();
zipped_out.write(xmlBinData, 0, xmlBinData.length);
zipped_out.flush();
}
zipped_out.finish();
}
out.close();
}
EDIT: Made some slight changes to it, but gives same error
// [... servlet handling ... ]
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=cierresZ_a_tangonet" + java.time.LocalDate.now() + ".zip");
// stream straight to client
ServletOutputStream out = response.getOutputStream();
ZipOutputStream zipped_outs = new ZipOutputStream(out);
for( each zip needed ){
//creates new zip inside big one
ZipEntry zipFile = new ZipEntry(salaActual + ".zip");
zipped_outs.putNextEntry(zipFile);
//opens stream for this new zip
ZipOutputStream zipped_out = new ZipOutputStream(zipped_outs);
for( each data block from db ){
//CREATION AND PROCESSING OF XML FILES AS ZIP ENTRIES
byte[] xmlBinData = xmlData.toString().getBytes();
zipped_out.write(xmlBinData, 0, xmlBinData.length);
zipped_out.flush();
}
//completes zip and closes it then goes for the next one
byte[] zipBinData = zipFile.toString().getBytes();
zipped_outs.write(zipBinData, 0, zipBinData.length);
zipped_outs.flush();
}
//closes the big zip filled with zips and returns
zipped_outs.finish();
out.close();
}