Last active
June 16, 2021 10:14
-
-
Save rickt/7817401 to your computer and use it in GitHub Desktop.
HOW-TO: inline-decompress a .bz2 file with Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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