title: How to generate zip / 7z archive on the fly in a HTTP server using Gin?
body: <p>I use <a href="https://github.com/gin-gonic/gin" rel="nofollow noreferrer">Gin</a> to create a HTTP server and I want to give a dynamically generated zip archive to the user.</p>
  
  <p>Theoretically I could first generate a zip file on a file system and then serve it. But that is really a bad way (to wait 5 mins before starting download). I want start giving it to a user immediately and push content as it is generated.</p>
  
  <p>I've found DataFromReader (<a href="https://gin-gonic.com/docs/examples/serving-data-from-reader/" rel="nofollow noreferrer">example</a>) but ContentLength is not known until archive is done.</p>
  
  <pre><code>func DownloadEndpoint(c *gin.Context) {
      ...
      c.DataFromReader(
          http.StatusOK,
          ContentLength,
          ContentType,
          Body,
          map[string]string{
              "Content-Disposition": "attachment; filename=\"archive.zip\""),
          },
      )
  }
  </code></pre>
  
  <p>How can I do that?</p>