Skip to content

Instantly share code, notes, and snippets.

@mjarkk
Created February 22, 2019 12:40
Show Gist options
  • Save mjarkk/8c291b9db7377d2a4f8a50f09d588ae2 to your computer and use it in GitHub Desktop.
Save mjarkk/8c291b9db7377d2a4f8a50f09d588ae2 to your computer and use it in GitHub Desktop.
Encode and compress a string to base64 and Decode it back to a string
func main() {
input := "KkktLgEEAAD//w=="
out, err := base64.StdEncoding.DecodeString(input)
if err != nil {
log.Panicln(err)
}
enflated, err := ioutil.ReadAll(flate.NewReader(bytes.NewReader(out)))
if err != nil {
log.Panicln(err)
}
content, err := url.QueryUnescape(string(enflated))
if err != nil {
log.Panicln(err)
}
fmt.Println(content)
}
func main() {
content := `test`
var buf bytes.Buffer
escapedURL := url.QueryEscape(content)
writter, err := flate.NewWriter(&buf, flate.BestCompression)
if err != nil {
log.Panicln(err)
}
writter.Write([]byte(escapedURL))
writter.Close()
outData := base64.StdEncoding.EncodeToString(buf.Bytes())
outData = outData + "" // make the go compiler happy
fmt.Println(outData)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment