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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀