Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Have a look at the <a href="http://commons.apache.org/compress/" rel="nofollow noreferrer">Apache Commons Compress</a> library, it provides the functionality you need.</p> <p>Of course "erickson" is right with his comment to your question. You will need the file content and not the <code>java.io.File</code> object. In my example I assume that you have a method <code>byte[] getTheContentFormSomewhere(int fileNummer)</code> which returns the file content (in memory) for the fileNummer-th file. -- Of course this function is poor design, but it is only for illustration.</p> <p>It should work a bit like this:</p> <pre><code>void compress(final OutputStream out) { ZipOutputStream zipOutputStream = new ZipOutputStream(out); zipOutputStream.setLevel(ZipOutputStream.STORED); for(int i = 0; i &lt; 10; i++) { //of course you need the file content of the i-th file byte[] oneFileContent = getTheContentFormSomewhere(i); addOneFileToZipArchive(zipOutputStream, "file"+i+"."txt", oneFileContent); } zipOutputStream.close(); } void addOneFileToZipArchive(final ZipOutputStream zipStream, String fileName, byte[] content) { ZipArchiveEntry zipEntry = new ZipArchiveEntry(fileName); zipStream.putNextEntry(zipEntry); zipStream.write(pdfBytes); zipStream.closeEntry(); } </code></pre> <p>Snipets of your http controller:</p> <pre><code>HttpServletResponse response ... response.setContentType("application/zip"); response.addHeader("Content-Disposition", "attachment; filename=\"compress.zip\""); response.addHeader("Content-Transfer-Encoding", "binary"); ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream(); compress(outputBuffer); response.getOutputStream().write(outputBuffer.toByteArray()); response.getOutputStream().flush(); outputBuffer.close(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload