title: Spring boot: Download zipped large files while they are being processed on the server
body: <p>In my app I'm zipping and then downloading larges files, the files are located in azure, so I read the files from a stream and then zip them one after another, so I can dowload the zip file after all files has been zipped, here's my code:</p>
  
  <pre><code>@RequestMapping(value = "{analyseId}/download", method = RequestMethod.GET, produces = "application/zip")
  public ResponseEntity&lt;Resource&gt; download(@PathVariable List&lt;String&gt; paths) throws IOException {
  
      String zipFileName = "zipFiles.zip";
      File zipFile = new File(zipFileName);
      FileOutputStream fos = new FileOutputStream(zipFile);
      ZipOutputStream zos = new ZipOutputStream(fos);
      for (String path : paths) {
          InputStream fis = azureDataLakeStoreService.readFile(path);
          addToZipFile(path , zos, fis);
      }
      zos.close();
      fos.close();
      BufferedInputStream zipFileInputStream = new BufferedInputStream(new FileInputStream(zipFile.getAbsolutePath()));
      InputStreamResource resource = new InputStreamResource(zipFileInputStream);
      zipFile.delete();
  
      return ResponseEntity.ok()
              .header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION)
              .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + zipFileName)
              .contentType(MediaType.parseMediaType("application/octet-stream"))
              .body(resource);
  }
  
  private static void addToZipFile(String path, ZipOutputStream zos, InputStream fis) throws IOException {
      ZipEntry zipEntry = new ZipEntry(FilenameUtils.getName(path));
      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>However on azure the request time out is set to 230 sec, and <a href="https://stackoverflow.com/questions/44820256/how-can-increase-azure-app-service-230-sec-request-time-out">cannot be changed</a>, however for big files it takes more than that to load and then zip the files on the server, so the connection with the client will be lost meanwhile.</p>
  
  <p>So my question is since I'm getting the data from a stream, can we do all these operations simultaneously, means getting the stream and download it as the same time and not waiting till getting the whole file, or if there any other idea can any body share it here please.</p>
  
  <p>Thanks.</p>