Created
November 30, 2013 14:16
-
-
Save mvrilo/7719648 to your computer and use it in GitHub Desktop.
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 ( | |
"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