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 系统 24h2 专业工作站版,浏览文件夹的图库,视频,图片之类的怎样删除?
  • ¥15 怎么把512还原为520格式
  • ¥15 MATLAB的动态模态分解出现错误,以CFX非定常模拟结果为快照
  • ¥15 求高通平台Softsim调试经验
  • ¥15 canal如何实现将mysql多张表(月表)采集入库到目标表中(一张表)?
  • ¥15 wpf ScrollViewer实现冻结左侧宽度w范围内的视图
  • ¥15 栅极驱动低侧烧毁MOSFET
  • ¥30 写segy数据时出错3
  • ¥100 linux下qt运行QCefView demo报错
  • ¥50 F1C100S下的红外解码IR_RX驱动问题