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

王林
Release: 2024-02-09 08:40:20
forward
652 people have browsed it

如何使用 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\""),
        },
    )
}
Copy after login

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()
}
Copy after login

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()
}
Copy after login

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!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!