title: Java zip files from streams instantly without using byte[]
body: <p>I want to compress multiples files into a zip files, I'm dealing with big files, and  then download them into the client, for the moment I'm using this:</p>
  
  <pre><code>@RequestMapping(value = "/download", method = RequestMethod.GET, produces = "application/zip")
  public ResponseEntity &lt;StreamingResponseBody&gt; getFile() throws Exception {
      File zippedFile = new File("test.zip");
      FileOutputStream fos = new FileOutputStream(zippedFile);
      ZipOutputStream zos = new ZipOutputStream(fos);
      InputStream[] streams = getStreamsFromAzure();
      for (InputStream stream: streams) {
          addToZipFile(zos, stream);
      }
      final InputStream fecFile = new FileInputStream(zippedFile);
      Long fileLength = zippedFile.length();
      StreamingResponseBody stream = outputStream - &gt;
          readAndWrite(fecFile, outputStream);
  
      return ResponseEntity.ok()
          .header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION)
          .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + "download.zip")
          .contentLength(fileLength)
          .contentType(MediaType.parseMediaType("application/zip"))
          .body(stream);
  }
  
  private void addToZipFile(ZipOutputStream zos, InputStream fis) throws IOException {
      ZipEntry zipEntry = new ZipEntry(generateFileName());
      zos.putNextEntry(zipEntry);
      byte[] bytes = new byte[1024];
      int length;
      while ((length = fis.read(bytes)) &gt;= 0) {
          zos.write(bytes, 0, length);
      }
      zos.closeEntry();
      fis.close();
  }
  </code></pre>
  
  <p>This take a lot of time before all files are zipped and then the downloading start, and for large files this kan take a lot of time, this is the line responsible for the delay:</p>
  
  <pre><code>while ((length = fis.read(bytes)) &gt;= 0) {
      zos.write(bytes, 0, length);
  }
  </code></pre>
  
  <p>So is there a way to download files immediately while their being zipped ?</p>