public static ByteArrayOutputStream requestsToZip( ServletContext context, String globalTitle, Date generationDate, ScrambleRequest[] scrambleRequests, String password, String generationUrl) throws IOException, DocumentException, ZipException { ByteArrayOutputStream baosZip = new ByteArrayOutputStream(); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); if (password != null) { parameters.setEncryptFiles(true); parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); parameters.setPassword(password); } parameters.setSourceExternalStream(true); ZipOutputStream zipOut = new ZipOutputStream(baosZip); HashMap<String, Boolean> seenTitles = new HashMap<String, Boolean>(); for (ScrambleRequest scrambleRequest : scrambleRequests) { String safeTitle = toFileSafeString(scrambleRequest.title); int salt = 0; String tempNewSafeTitle = safeTitle; while (seenTitles.get(tempNewSafeTitle) != null) { tempNewSafeTitle = safeTitle + " (" + (++salt) + ")"; } safeTitle = tempNewSafeTitle; seenTitles.put(safeTitle, true); String pdfFileName = "pdf/" + safeTitle + ".pdf"; parameters.setFileNameInZip(pdfFileName); zipOut.putNextEntry(null, parameters); PdfReader pdfReader = createPdf(globalTitle, generationDate, scrambleRequest); byte[] b = new byte[(int) pdfReader.getFileLength()]; pdfReader.getSafeFile().readFully(b); zipOut.write(b); zipOut.closeEntry(); String txtFileName = "txt/" + safeTitle + ".txt"; parameters.setFileNameInZip(txtFileName); zipOut.putNextEntry(null, parameters); zipOut.write(join(stripNewlines(scrambleRequest.getAllScrambles()), "\r\n").getBytes()); zipOut.closeEntry(); } String safeGlobalTitle = toFileSafeString(globalTitle); String jsonFileName = safeGlobalTitle + ".json"; parameters.setFileNameInZip(jsonFileName); zipOut.putNextEntry(null, parameters); HashMap<String, Object> jsonObj = new HashMap<String, Object>(); jsonObj.put("sheets", scrambleRequests); jsonObj.put("competitionName", globalTitle); jsonObj.put("version", Utils.getProjectName() + "-" + Utils.getVersion()); jsonObj.put("generationDate", generationDate); jsonObj.put("generationUrl", generationUrl); String json = GSON.toJson(jsonObj); zipOut.write(json.getBytes()); zipOut.closeEntry(); String jsonpFileName = safeGlobalTitle + ".jsonp"; parameters.setFileNameInZip(jsonpFileName); zipOut.putNextEntry(null, parameters); String jsonp = "var SCRAMBLES_JSON = " + json + ";"; zipOut.write(jsonp.getBytes()); zipOut.closeEntry(); parameters.setFileNameInZip(safeGlobalTitle + ".html"); zipOut.putNextEntry(null, parameters); InputStream is = context.getResourceAsStream(HTML_SCRAMBLE_VIEWER); BufferedReader in = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line; while ((line = in.readLine()) != null) { line = line.replaceAll("%SCRAMBLES_JSONP_FILENAME%", jsonpFileName); sb.append(line).append("\n"); } zipOut.write(sb.toString().getBytes()); zipOut.closeEntry(); parameters.setFileNameInZip(safeGlobalTitle + ".pdf"); zipOut.putNextEntry(null, parameters); // Note that we're not passing the password into this function. It seems pretty silly // to put a password protected pdf inside of a password protected zip file. ByteArrayOutputStream baos = requestsToPdf(globalTitle, generationDate, scrambleRequests, null); zipOut.write(baos.toByteArray()); zipOut.closeEntry(); zipOut.finish(); zipOut.close(); return baosZip; }