将Golang转换为JSON.GZ并写入文件。

huangapple go评论85阅读模式
英文:

Golang: Convert to JSON.GZ and write to file

问题

尝试使用我的数据实现以下输出:

  1. 将数据转换为JSON字符串并写入文件:output.json(这部分已经实现)
  2. 对JSON字符串进行Gzip压缩,并将其写入json.gz文件:output.json.gz(不起作用

代码运行正常,并成功写入两个文件。但是当我尝试解压缩gzipped文件时,会出现以下错误:Data error in 'output.json'. File is broken

以下是代码:

package main

import (
	"bytes"
	"compress/gzip"
	"encoding/json"
	"fmt"
	"io/ioutil"
)

type Generic struct {
	Name string
	Cool bool
	Rank int
}

func main() {
	generic := Generic{"Golang", true, 100}
	fileJson, _ := json.Marshal(generic)
	err := ioutil.WriteFile("output.json", fileJson, 0644)
	if err != nil {
		fmt.Printf("WriteFileJson ERROR: %+v", err)
	}

	var fileGZ bytes.Buffer
	zipper := gzip.NewWriter(&fileGZ)
	defer zipper.Close()
	_, err = zipper.Write([]byte(string(fileJson)))
	if err != nil {
		fmt.Printf("zipper.Write ERROR: %+v", err)
	}
	err = ioutil.WriteFile("output.json.gz", []byte(fileGZ.String()), 0644)
	if err != nil {
		fmt.Printf("WriteFileGZ ERROR: %+v", err)
	}
}

我错过了什么?

英文:

Trying to accomplish the following output with my data:

  1. Convert to JSON string and write to file: output.json (this part is working)
  2. Gzip Compress the JSON string and write that to a json.gz file: output.json.gz (NOT WORKING)

The code runs fine and writes to both files. But the gzipped file gives this error when I try to unzip it: Data error in 'output.json'. File is broken

Here's the code:

package main

import (
	"bytes"
	"compress/gzip"
	"encoding/json"
	"fmt"
	"io/ioutil"
)

type Generic struct {
	Name string
	Cool bool
	Rank int
}

func main() {
	generic := Generic{"Golang", true, 100}
	fileJson, _ := json.Marshal(generic)
	err := ioutil.WriteFile("output.json", fileJson, 0644)
	if err != nil {
		fmt.Printf("WriteFileJson ERROR: %+v", err)
	}

	var fileGZ bytes.Buffer
	zipper := gzip.NewWriter(&fileGZ)
	defer zipper.Close()
	_, err = zipper.Write([]byte(string(fileJson)))
	if err != nil {
		fmt.Printf("zipper.Write ERROR: %+v", err)
	}
	err = ioutil.WriteFile("output.json.gz", []byte(fileGZ.String()), 0644)
	if err != nil {
		fmt.Printf("WriteFileGZ ERROR: %+v", err)
	}
}

What did I miss?

答案1

得分: 8

你需要在完成写入操作后立即调用zipper.Close()

http://play.golang.org/p/xNeMg3aXxO

_, err = zipper.Write(fileJson)
if err != nil {
    log.Fatalf("zipper.Write ERROR: %+v", err)
}
err := zipper.Close() // 显式调用并检查错误

调用`defer zipper.Close()`会在主函数结束时触发调用。在调用`.Close()`之前,数据将被写入一个中间缓冲区而不是刷新到实际文件中。

<details>
<summary>英文:</summary>

You need to call zipper.Close() immediately after finishing writing

http://play.golang.org/p/xNeMg3aXxO

	_, err = zipper.Write(fileJson)
	if err != nil {
		log.Fatalf(&quot;zipper.Write ERROR: %+v&quot;, err)
	}
	err := zipper.Close() // call it explicitly and check error 

Calling `defer zipper.Close()` would trigger the call at the end of the main function. Until you call `.Close()` the data is being written to an intermediate buffer and not flushed to the actual file. 

</details>



huangapple
  • 本文由 发表于 2014年8月19日 05:44:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/25372506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定