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()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)