Skip to content

Instantly share code, notes, and snippets.

@rickt
Last active June 16, 2021 10:14
Show Gist options
  • Save rickt/7817401 to your computer and use it in GitHub Desktop.
Save rickt/7817401 to your computer and use it in GitHub Desktop.
HOW-TO: inline-decompress a .bz2 file with Go
package main
import (
"bufio"
"compress/bzip2"
"fmt"
"io"
"os"
)
var (
// the bzip file
bzipfile string = "/home/foo/bzipfile.bz2"
)
func main() {
// open the file
f, err := os.OpenFile(bzipfile, 0, 0)
if err != nil {
panic(err)
}
defer f.Close()
// create a reader
br := bufio.NewReader(f)
// create a bzip2.reader, using the reader we just created
cr := bzip2.NewReader(br)
// lets do this. parse the file.
err = parsefile(cr)
}
func parsefile(bzipfile io.Reader) error {
var ll string
// create a reader, using the bzip2.reader we were passed
d := bufio.NewReader(bzipfile)
// create a scanner
s := bufio.NewScanner(d)
// scan the file! until Scan() returns "EOF", print out each line
for s.Scan() {
ll = s.Text()
fmt.Printf("ll=%s\n", ll)
}
// we're done. return.
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment