2

I want to compress the dynamic created content and write to ServletOutputStream directly, without saving it as a file on the server before compressing.

For example, I created an Excel Workbook and a StringBuffer that includes strings with the SQL template. I don't want to save the dynamic content to .xlsx and .sql file on the server before zipping the files and writing to ServletOutputStream for downloading.

Sample Code:

ServletOutputStream out = response.getOutputStream();
workbook.write(byteArrayOutputStream);    
zipIt(byteArrayOutputStream,out);

public static boolean zipIt(ByteArrayOutputStream input, ServletOutputStream output) {
        try {
            ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(output));
            ZipEntry zipEntry = new ZipEntry("test.xlsx");
            zos.putNextEntry(zipEntry);
            if (input != null) {
                zipEntry.setSize(input.size());
                zos.write(input.toByteArray());
                zos.closeEntry();
            }
        } catch (IOException e) {
            logger.error("error {}", e);
            return false;
        }
        return true;
    }
08Dc91wk
  • 4,254
  • 8
  • 34
  • 67
Hosein Masbough
  • 431
  • 1
  • 4
  • 19

1 Answers1

5

Create an HttpServlet and in the doGet() or doPost() method create a ZipOutputStream initialized with the ServletOutputStream and write directly to it:

    resp.setContentType("application/zip");
    // Indicate that a file is being sent back:
    resp.setHeader("Content-Disposition", "attachment;filename=test.zip");

    // workbook.write() closes the stream, so we first have to
    // write it to a "buffer", a ByteArrayOutputStream
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    workbook.write(baos);
    byte[] data = baos.toByteArray();

    try (ZipOutputStream out = new ZipOutputStream(resp.getOutputStream())) {
        // Here you can add your content to the zip 

        ZipEntry e = new ZipEntry("test.xlsx");
        // Configure the zip entry, the properties of the file
        e.setSize(data.length);
        e.setTime(System.currentTimeMillis());
        // etc.
        out.putNextEntry(e);
        // And the content of the XLSX:
        out.write(data);
        out.closeEntry();

        // You may add other files here if you want to

        out.finish();
    } catch (Exception e) {
        // Handle the exception
    }
}
icza
  • 389,944
  • 63
  • 907
  • 827
  • I get e `Exception` from `out.closeEntry()` : `java.io.IOException: Stream closed` – Hosein Masbough Sep 30 '14 at 05:51
  • 1
    Well that means `workbook.write()` closes the stream, so you first have to write it to a "buffer", a `ByteArrayOutputStream`. Edited my answer. – icza Sep 30 '14 at 06:16
  • I was having trouble with this exact problem, getting corrupted .zip files. The call to `out.finish()` was the missing critical piece of info I needed. Thanks. – David Sykes Aug 25 '21 at 01:06