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 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?