Created
July 9, 2019 10:49
-
-
Save literadix/abf2e7b053968219120be5c5ca234fdd to your computer and use it in GitHub Desktop.
golang write to gzip file
This file contains hidden or 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
/* | |
This is an example of a golang gzip writer program, | |
which appends data to a file. | |
*/ | |
package main | |
import ( | |
"bufio" | |
"compress/gzip" | |
"log" | |
// "fmt" | |
"os" | |
) | |
type F struct { | |
f *os.File | |
gf *gzip.Writer | |
fw *bufio.Writer | |
} | |
func CreateGZ(s string) (f F) { | |
fi, err := os.OpenFile(s, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660) | |
if err != nil { | |
log.Printf("Error in Create\n") | |
panic(err) | |
} | |
gf := gzip.NewWriter(fi) | |
fw := bufio.NewWriter(gf) | |
f = F{fi, gf, fw} | |
return | |
} | |
func WriteGZ(f F, s string) { | |
(f.fw).WriteString(s) | |
} | |
func CloseGZ(f F) { | |
f.fw.Flush() | |
// Close the gzip first. | |
f.gf.Close() | |
f.f.Close() | |
} | |
func main() { | |
f := CreateGZ("Append.gz") | |
WriteGZ(f, "gzipWriter.go created this file.\n") | |
CloseGZ(f) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment