LoginSignup
0
0

More than 5 years have passed since last update.

gzip のやり方

Last updated at Posted at 2017-09-14

普通に文字列 (string) から gzip を生成するやり方
エラーハンドリングは省略

import (
  "bytes"
  "compress/gzip"
)

func makeGzip(body string) []byte {
  var b bytes.Buffer
  gw := gzip.NewWriter(&b)
  _, err := gw.Write([]byte(body)); if err != nil {
    ...
  }
  gw.Flush()
  gw.Close()
  return b.Bytes()
}

gw.Close() はファイルストリームのクローズみたいな挙動ではなく,
gzip のフッターデータ書き込みなので defer 等で最後に回そうとすると
不正なデータになってしまうので注意. (とりあえず解凍はできない)
Close() は Flush() の処理を含んでいるかもしれませんが自信がないです.

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0