go
40 lines · 8 steps
Streaming a ZIP download in Gin
A Gin handler assembles project reports into a ZIP and streams it straight to the client without buffering the whole archive.
Explained by
highlit
1func (h *ExportHandler) BulkExport(c *gin.Context) {
2 projectID := c.Param("projectID")
3
4 reports, err := h.reports.ListByProject(c.Request.Context(), projectID)
5 if err != nil {
6 c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "failed to load reports"})
7 return
8 }
9 if len(reports) == 0 {
10 c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": "no reports to export"})
11 return
12 }
13
14 filename := fmt.Sprintf("reports-%s-%s.zip", projectID, time.Now().Format("20060102"))
15 c.Header("Content-Type", "application/zip")
16 c.Header("Content-Disposition", fmt.Sprintf(`attachment; filename=%q`, filename))
17 c.Header("X-Content-Type-Options", "nosniff")
18
19 zw := zip.NewWriter(c.Writer)
20 defer zw.Close()
21
22 for _, r := range reports {
23 w, err := zw.CreateHeader(&zip.FileHeader{
24 Name: fmt.Sprintf("%s/%s.csv", r.Category, r.Slug),
25 Method: zip.Deflate,
26 Modified: r.UpdatedAt,
27 })
28 if err != nil {
29 c.Error(err)
30 return
31 }
32
33 if err := h.reports.StreamCSV(c.Request.Context(), r.ID, w); err != nil {
34 c.Error(err)
35 return
36 }
37
38 c.Writer.Flush()
39 }
40}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Writing the zip.Writer directly onto the response writer avoids buffering the entire archive in memory.
- 2Setting download headers before writing any body is essential because headers can't change once bytes are flushed.
- 3Validate that there's something to send and fail fast before committing to a streaming response.
Related explainers
rust
use axum::{ extract::State, response::sse::{Event, KeepAlive, Sse}, Json,
Proxying an SSE chat stream in Axum
server-sent-events
streaming
async-generators
Advanced
10 steps
go
package middleware import ( "net/http"
Localized validation errors in Gin
middleware
internationalization
validation
Intermediate
8 steps
javascript
async function uploadInBatches(records, uploadFn, { batchSize = 100, concurrency = 3 } = {}) { const batches = []; for (let i = 0; i < records.length; i += batchSize) { batches.push(records.slice(i, i + batchSize));
Uploading records with bounded concurrency
concurrency
worker-pool
async-await
Advanced
8 steps
java
package com.example.validation; import java.util.List; import java.util.stream.Collectors;
Validating JSON payloads against a schema in Java
json-schema
validation
recursion
Intermediate
8 steps
go
func ListProducts(c *gin.Context) { allowed := map[string]string{ "category": "category", "brand": "brand",
Safe query filtering in a Gin handler
input-validation
whitelisting
query-building
Intermediate
7 steps
php
<?php namespace App\Http\Controllers;
Server-Sent Events in Laravel
server-sent-events
streaming
long-polling
Advanced
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/streaming-a-zip-download-in-gin-explained-go-4922/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.