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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With