doulan8330 2019-08-09 11:37
浏览 534
已采纳

如何使用Gin在HTTP服务器中动态生成zip / 7z存档?

I use Gin to create a HTTP server and I want to give a dynamically generated zip archive to the user.

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.

I've found DataFromReader (example) but ContentLength is not known until archive is done.

func DownloadEndpoint(c *gin.Context) {
    ...
    c.DataFromReader(
        http.StatusOK,
        ContentLength,
        ContentType,
        Body,
        map[string]string{
            "Content-Disposition": "attachment; filename=\"archive.zip\""),
        },
    )
}

How can I do that?

  • 写回答

1条回答 默认 最新

  • douju4278 2019-08-09 16:54
    关注

    Using the stream method and archive/zip you can create zip on the fly and stream them to the server.

    package main
    
    import (
        "os"
    
        "archive/zip"
    
        "github.com/gin-gonic/gin"
    )
    
    func main() {
    
        r := gin.Default()
        r.GET("/", func(c *gin.Context) {
    
            c.Writer.Header().Set("Content-type", "application/octet-stream")
            c.Stream(func(w io.Writer) bool {
    
                // Create a zip archive.
                ar := zip.NewWriter(w)
    
                file1, _ := os.Open("filename1")
                file2, _ := os.Open("filename2")
                c.Writer.Header().Set("Content-Disposition", "attachment; filename='filename.zip'")
    
                f1, _ := ar.Create("filename1")
                io.Copy(f1, file1)
                f2, _ := ar.Create("filename2")
                io.Copy(f2, file2)
    
                ar.Close()
    
                return false
            })
        })
        r.Run()
    }
    

    By using directly ResponseWriter

    package main
    
    import (
        "io"
        "os"
    
        "archive/zip"
    
        "github.com/gin-gonic/gin"
    )
    
    
    func main() {
    
        r := gin.Default()
        r.GET("/", func(c *gin.Context) {
            c.Writer.Header().Set("Content-type", "application/octet-stream")
            c.Writer.Header().Set("Content-Disposition", "attachment; filename='filename.zip'")
            ar :=  zip.NewWriter(c.Writer)
            file1, _ := os.Open("filename1")
            file2, _ := os.Open("filename2")
            f1, _ := ar.Create("filename1")
            io.Copy(f1, file1)
            f2, _ := ar.Create("filename1")
            io.Copy(f1, file2)
            ar.Close()
        })
        r.Run()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵