Este código gera o arquivo em pdf e em seguida faz o download, porém, antes eu gostaria de compactar em .zip.
public class GerarRelatorio {
private HttpServletResponse response;
private FacesContext context;
public GerarRelatorio() {
this.context = FacesContext.getCurrentInstance();
this.response = (HttpServletResponse) context.getExternalContext().getResponse();
}
public void gerarRelatorio(String nomeRelatorio, Map<String, Object> parametros, String nomeArquivo) throws SQLException, ServletException, IOException, JRException {
String relatorio = "/WEB-INF/jasper/"+nomeRelatorio+".jasper";
InputStream reportStream = context.getExternalContext().getResourceAsStream(relatorio);
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "attachment;filename="+nomeArquivo+".pdf");
ServletOutputStream servletOutputStream = response.getOutputStream();
Connection connection = null;
try {
connection = new ConnectionFactory().recuperarConexao();
JasperRunManager.runReportToPdfStream(reportStream, response.getOutputStream(), parametros, connection);
} catch (SQLException e) {
throw new ServletException(e);
} finally {
connection.close();
}
FacesContext.getCurrentInstance().responseComplete();
servletOutputStream.flush();
servletOutputStream.close();
}
}