需要zip归档文件中的io.ReaderAt条目(该条目是一个嵌套的.xlsx文件) [英] Need io.ReaderAt from zip archive entry (the entry is a nested .xlsx file)

查看:387
本文介绍了需要zip归档文件中的io.ReaderAt条目(该条目是一个嵌套的.xlsx文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我以这样一个事实来说这个问题,我已经知道一个excel 2007文件本身就是一个.zip文件,重命名为.xlsx。

好吧,现在你知道这是交易。我试图从内存中的.zip压缩文件中提取Excel 2007文件。我不能(更确切地说,不想)将整个存档提取到磁盘,然后从那里使用.xlsx文件。

问题是我们阅读excel 2007文件的方法需要一个ReadAt方法(例如 io.ReaderAt )。不幸的是,存档/压缩包暴露了压缩文件条目的接口返回 io.ReadCloser



有什么办法可以解决这种情况?再次,我想在内存中完成所有这些操作,而无需刷新磁盘。

解决方案

因为ZIP格式不会如果没有先解压缩整个文件,就不允许实现ReadAt,那么您需要做到这一点。



这并不意味着您必须将其保存到磁盘,但相反,您可以使用字节软件包中的 Reader 将其解压缩到内存并在其中进行处理。

  // ReadAll从readCloser读取,直到EOF返回数据为[]字节
b,err:= ioutil.ReadAll(readCloser )// readCloser是zip-package
中的一个,如果err!= nil {
panic(err)
}

// bytes.Reader implements io.Reader,io.ReaderAt等等所有你需要的!
readerAt:= bytes.NewReader(b)


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)

这篇关于需要zip归档文件中的io.ReaderAt条目(该条目是一个嵌套的.xlsx文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆