duanke1984
2014-08-18 21:44
浏览 230

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

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?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanquyong8164 2014-08-18 21:55
    已采纳

    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("zipper.Write ERROR: %+v", 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.

    已采纳该答案
    打赏 评论
  • 遥空与清景 2020-09-24 17:46

    这个生成的json文件不能追加啊,只是在第一条记录上一直更新,怎么解决啊

    打赏 评论

相关推荐 更多相似问题