Skip to content

Instantly share code, notes, and snippets.

@mvrilo
Created November 30, 2013 14:16
Show Gist options
  • Save mvrilo/7719648 to your computer and use it in GitHub Desktop.
Save mvrilo/7719648 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"compress/gzip"
"io/ioutil"
)
type File struct {
path string
}
func (f *File) Read() ([]byte, error) {
b, err := ioutil.ReadFile(f.path)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(b)
g, err := gzip.NewReader(buf)
if err != nil {
return nil, err
}
defer g.Close()
body := make([]byte, len(b))
_, err = g.Read(body)
if err != nil {
return nil, err
}
return body, nil
}
func (f *File) Write(data []byte) error {
var b bytes.Buffer
w := gzip.NewWriter(&b)
_, err := w.Write(data)
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return ioutil.WriteFile(f.path, b.Bytes(), 0666)
}
func Open(path string) *File {
return &File{path}
}
func main() {
f := Open("hola.txt.gz")
f.Write([]byte("meh"))
//str, _ := f.Read()
//println(string(str))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment