Home  >  Article  >  Backend Development  >  How to generate zip/7z archive on the fly in HTTP server using Gin?

How to generate zip/7z archive on the fly in HTTP server using Gin?

王林
王林forward
2024-02-09 08:40:20620browse

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

php editor Apple brings you a concise guide to using Gin to instantly generate zip/7z archives in HTTP servers. Gin is a lightweight Go language framework with high performance and ease of use. This article will introduce how to use Gin to handle HTTP requests and generate zip and 7z archive files by calling system commands and third-party libraries. Whether you are a beginner or an experienced developer, following this tutorial, you will be able to easily implement this feature and add more value to your web application. let's start!

Question content

I am using Gin to create an HTTP server and I want to serve dynamically generated zip archives to users.

Theoretically, I could first generate a zip file on the file system and then serve it. But this is really a bad approach (wait 5 minutes before starting the download). I want to start serving it to users immediately and push content as it is generated.

I found the DataFromReader (example) but don't know the ContentLength until the archive is complete.

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 this?

Workaround

Using the streaming method and archive/zip you can dynamically create a zip and stream it 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()
}

Use ResponseWriter directly

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()
}

The above is the detailed content of How to generate zip/7z archive on the fly in HTTP server using Gin?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete