Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need io.ReaderAt from zip archive entry (the entry is a nested .xlsx file)

Tags:

zip

go

excel-2007

Let me preface this question with the fact that I already know that an excel 2007 file is itself a .zip file, renamed to .xlsx.

Ok, now that you know that here's the deal. I'm trying to extract an Excel 2007 file from within a .zip archive all in memory. I can't (rather, I really don't want to) extract the whole archive to disk and then work with the .xlsx file from there.

The problem is that our method of reading excel 2007 files requires a ReadAt method (such as what is defined by io.ReaderAt). Unfortunately, the archive/zip package exposes an interface for zip file entries that only gives back io.ReadCloser.

Is there any way to get around this situation? Again, I'd like to do this all in memory, without flushing to disk at all.

like image 459
mdwhatcott Avatar asked Oct 28 '13 18:10

mdwhatcott


1 Answers

Because the ZIP format doesn't allow an implementation of ReadAt without first uncompressing the entire file, you will need to do exactly that.

That doesn't mean you must save it to disk, but instead you can decompress it to memory and work on it from there using Reader in the bytes package:

// ReadAll reads from readCloser until EOF and returns the data as a []byte
b, err := ioutil.ReadAll(readCloser) // The readCloser is the one from the zip-package
if err != nil {
    panic(err)
}

// bytes.Reader implements io.Reader, io.ReaderAt, etc. All you need!
readerAt := bytes.NewReader(b)
like image 114
ANisus Avatar answered Sep 22 '22 19:09

ANisus