gpt4 book ai didi

xml - Golang:将xml转换为gzip时出现问题

转载 作者:行者123 更新时间:2023-12-01 22:34:53 25 4
gpt4 key购买 nike

我正在开发一个使用golang将xml文件压缩为gzip的程序。

但是该程序无法生成文件,但是当我尝试将.txt文件转换为gzip时,它的确生成了输出。
这是我的程序:

package main

import (
"bytes"
"compress/gzip"
"fmt"
"io"
"log"
"os"
)

type Notes struct {
To string `xml:"to"`
From string `xml:"from"`
Heading string `xml:"heading"`
Body string `xml:"body"`
}

func main() {
var buf bytes.Buffer
zw := gzip.NewWriter(&buf)

// Setting the Header fields is optional.
zw.Name = "new.xml"

_, err := zw.Write([]byte("Compressing"))
if err != nil {
log.Fatal(err)
}

if err := zw.Close(); err != nil {
log.Fatal(err)
}

zr, err := gzip.NewReader(&buf)
if err != nil {
log.Fatal(err)
}

fmt.Printf("Name: %s", zr.Name)

if _, err := io.Copy(os.Stdout, zr); err != nil {
log.Fatal(err)
}

if err := zr.Close(); err != nil {
log.Fatal(err)
}

}


我要如何生成所需的.gz文件。

最佳答案

现在,zw.Write()调用会将(压缩的)数据写入buff。因为,相反,您想将其写入文件,所以您应该创建可以执行此操作的文件。

最简单的方法是使用os.Create()。此函数返回一个*os.File,它实现了io.Writer

结果代码如下所示:

package main

import (
"compress/gzip"
"log"
"os"
)

func main() {
// This creates a file and returns an implementation of io.Writer
fileWriter, err := os.Create("./file.gz")
if err != nil {
log.Println(err)
return
}
defer fileWriter.Close()

// Use the io.Writer to create the gzip.Writer.
zw := gzip.NewWriter(fileWriter)
defer zw.Close()

// Setting the Header fields is optional.
zw.Name = "new.xml"

// When gzip.Writer.Write is called, it will pass on the data to the Write func of the io.Writer we passed on line 17.
// If there is an error writing to the actual file, it will be returned.
_, err = zw.Write([]byte("Compressing"))
if err != nil {
log.Println(err)
return
}
}


这种编写作者的方式使更改工作方式变得非常容易,而无需更改太多代码。您可以更进一步,因为 *xml.Encoder也是 io.Writer的实现,就像 io.Writer一样,它以另一个 *gzip.Writer作为参数。因此,要实际生成XML并将其写入文件并将其gzip压缩,只需执行以下操作:

package main

import (
"compress/gzip"
"encoding/xml"
"log"
"os"
)

type Notes struct {
To string `xml:"to"`
From string `xml:"from"`
Heading string `xml:"heading"`
Body string `xml:"body"`
}

func main() {
// This creates a file and returns *os.File, an implementation of io.Writer
fileWriter, err := os.Create("./notes.gz")
if err != nil {
log.Println(err)
return
}
defer fileWriter.Close()

// Use the io.Writer to create the gzip.Writer.
zw := gzip.NewWriter(fileWriter)
defer zw.Close()

// Setting the Header fields is optional.
zw.Name = "notes.xml"

notes := []Notes{
{
To: "Alice",
From: "Bob",
Heading: "Hi",
Body: "Hey Alice, how are you?",
},
{
To: "Bob",
From: "Alice",
Heading: "Re: Hi",
Body: "Hi Bob! I'm fine, thnx.",
},
}

// Create the xml.Encoder, using the gzip.Writer (which implements io.Writer).
xmlWriter := xml.NewEncoder(zw)
// So now, we have an xml.Encoder which writes to a gzip.Writer which writes to io.File.

// This call to Encode() will generate the XML, pass that to gzip.Writer.Write, which passes it to os.File.Write.
err = xmlWriter.Encode(notes)
if err != nil {
log.Println(err)
return
}
}


这种撰写作者(以及读者)的方式非常强大。您可以在很多地方找到它,这对于“分层”作家来说非常容易。

关于xml - Golang:将xml转换为gzip时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59785830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com