How to unzip io.ReadCloser?

Zip archives require random access for reading, so it's hard to stream bytes. In particular, see the source for zip.Reader.init here: http://golang.org/src/pkg/archive/zip/reader.go?s=1265:1323#L59 . The first thing it does is call readDirectoryEnd which reads from near the end of the file.

Can you use a different compression method (for example, gzip)? Then you can use gzip.NewReader(resp.Body) to stream the data.


Are you looking for something like below:

var ioReader io.Reader
buff := bytes.NewBuffer([]byte{})
size, err := io.Copy(buff, ioReader)
if err != nil {
    return err
}

reader := bytes.NewReader(buff.Bytes())

// Open a zip archive for reading.
zipReader, err := zip.NewReader(reader, size)

Tags:

Zip

Go