How to use compress/zlib?

400 views
Skip to first unread message

Ondekoza

unread,
Jan 10, 2011, 8:24:43 AM1/10/11
to golang-nuts
On

http://golang.org/pkg/compress/zlib/

I find the documentation for the zlib-package. For reading a
compressed chunk of data the example is:


r, err := zlib.NewReader(&b)
io.Copy(os.Stdout, r)
r.Close()


But how do I wrap the open'ing and close'ing of a file around this
excerpt, so that I can read a compressed file?
I experimented with a generic Reader from os and from the file-
package, created in the tutorial, but to no avail!

Answers appreciated.

TIA, ondekoza

roger peppe

unread,
Jan 10, 2011, 8:32:03 AM1/10/11
to Ondekoza, golang-nuts

something like:

f, err := os.Open("file.Z", os.O_RDONLY, 0)
r, err := zlib.NewReader(f)
io.Copy(os.Stdout, r)

with appropriate error checking inserted.

i haven't tested this code.

Ondekoza

unread,
Jan 10, 2011, 2:25:01 PM1/10/11
to golang-nuts
Hi Roger,

thank you for your answer. Using your post I could solve my problem;
I had a 'gz' file, and I thought it was a zlib file, but in fact it
worked with
compress/gzip. Thank you very much.

Reply all
Reply to author
Forward
0 new messages