我试图用Go中的archive/zip包压缩一大块字节.然而,我一点也不明白.有没有什么例子可以说明这是如何做到的?有没有任何关于这个神秘的包的解释?

推荐答案

多亏了Jamessan,我找到了这个示例(这并不能完全吸引您的眼球).

以下是我得出的结果:

func (this *Zipnik) zipData() {

    // Create a buffer to write our archive to.
    fmt.Println("we are in the zipData function")
    buf := new(bytes.Buffer)

    // Create a new zip archive.
    zipWriter := zip.NewWriter(buf)

    // Add some files to the archive.
    var files = []struct {
        Name, Body string
    }{
        {"readme.txt", "This archive contains some text files."},
        {"gopher.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"},
        {"todo.txt", "Get animal handling licence.\nWrite more examples."},
    }
    for _, file := range files {
    zipFile, err := zipWriter.Create(file.Name)
        if err != nil {
            fmt.Println(err)
        }
        _, err = zipFile.Write([]byte(file.Body))  
        if err != nil {
            fmt.Println(err)
        }
    }

    // Make sure to check the error on Close.
    err := zipWriter.Close()
    if err != nil {
        fmt.Println(err)
    }

    //write the zipped file to the disk
    ioutil.WriteFile("Hello.zip", buf.Bytes(), 0777)    

}

我希望它对您有用:)

Go相关问答推荐

向路由发送获取请求时Golang中出现404错误

调用API时使用nginx作为反向代理时从nginx获取502坏网关

编辑时保留YAML文件中的单引号

你能把用户界面文件中的GTK4应用程序窗口添加到GTK4应用程序中吗?

golang 的持久隐蔽服务

需要类型[]*structpb.Value(GCP Golang客户端库;aiPlatform)

如何在Golang中使用ECHO服务器实现Socket.IO服务器

Golang String

无法获取RPC描述符

`docker system df` 与 `/system/df` (docker api 端点)

无法在go中为docker容器写入有效的挂载路径

Golang 发送Post请求出现400错误

以编程方式取消 pyspark dataproc 批处理作业(job)

加载 docker 镜像失败

Golang代码判断第一个词是否可以从第二个词形成

如何使用 Go 代理状态为 OK 的预检请求?

如何过滤来自 fsnotify 的重复系统消息

Gorm 在保存/创建时序列化 struct

Go 中 SDL Surface 的 OpenGL 纹理

Golang 查询扫描未将查询正确扫描到 struct 中