Read Gzip file content with golang
16 Sep 2015With one of my recent tasks, I had to read Gzipped JSON files from a directory.
This proved to be pretty easy (as a lot of things with Go are), but I thought
it’s pretty useful to blog about it since many people likely need this
package main
import (
"bufio"
"compress/gzip"
"log"
"os"
)
func main() {
filename := "your-file-name.gz"
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
gz, err := gzip.NewReader(file)
if err != nil {
log.Fatal(err)
}
defer file.Close()
defer gz.Close()
scanner := bufio.NewScanner(gz)
}
From there, you can do with scanner
what ever you want, I needed to read the
lines one by one in order to parse each of the lines as JSON but obviously, you
can do whatever you want with it.
Keep Hacking!