0

I'm trying to download a zip file in spring boot. Using HttpServletResponse output stream. We will be reading a file from local, zip it, inside StreamingResponseBody and add it in the return statement of ResponseEntity.body(StreamingResponseBody). We require content length to show progress bar in UI. We can get zipEntry.getCompressedSize() for the content length. But as soon as the zip file is written spring boot sending response headers to UI and I'll be getting content length only after I close zipoutputstream. Because of this I'm getting 0 as content length as a header in UI. Can you please help me to properly add content length to response headers.

Thanks in advance.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • 1
    Zip to filesystem. Close stream, determine size, then stream to client. If you really want you need an intermediate step. – M. Deinum Oct 06 '22 at 07:27
  • Hi @M.Deinum we are wrapping the zip similar to https://medium.com/swlh/streaming-data-with-spring-boot-restful-web-service-87522511c071 using lamda. Without filesystem is it possible to set content length. – Venkatasai Janaparapu Oct 06 '22 at 07:38
  • You would need to store it in memory in a byte[] or `ByteArrayOutputStream` however that is really memory intensive and might lead to OOM. – M. Deinum Oct 06 '22 at 07:40
  • 1
    If you are using the code from the link, you are actually using a file system (as a `File` is just that, a resource on the filesystem). – M. Deinum Oct 06 '22 at 07:45

0 Answers0