doufeinai6081 2019-09-17 07:18
浏览 88
已采纳

如果我的类型作为流没有意义,我应该实现io.Reader / io.Writer吗?

Question regarding zip and io.Reader/io.Writer. As far as I understand, one of the purpose of io/Reader/io.Writer is streaming. But should I implement one of these if my type does not really make sense "as chunks"?

For more details:

Lets say I have this struct.

type MyZip struct {
    file1, file2 []byte
}

MyZip represents a particular structured zip. Let's say for example it represents a zip file containing exactly a file named file1 and a file named file2. MyZip has the responsibility of parsing a zip file to extract these two files into two []byte fields. It also should handle the other way around (turning these two []byte fields as two files named test1 and test2 archived into a zip file).

As far as I understand, the package archive/zip does not allow to decompress a zip file as a stream. We have to fully load the zip in memory or as a file and decompress afterwards.

So to refine my question, does it make sense for MyZip to implement io.Reader/io.Writer for reading/writing from/to the final zip file?

As said above, as I cannot extract the two files on the fly, I would have to add some sort of buffer to MyZip and just read/write from/to this buffer. So the zip would anyway be fully stored in memory before being streamed. Is it a counter indication for not using io.Reader/io.Writer?

Thanks a lot for shedding light!

  • 写回答

1条回答 默认 最新

  • dongqishun6409 2019-09-17 07:26
    关注

    As far as I understand, the package archive/zip does not allow to decompress a zip file as a stream. We have to fully load the zip in memory or as a file and decompress afterwards.

    Wrong. Some metadata needs to be loaded into memory, yes, but you do not need to load everything into memory. You can extract individual files from a zip archive. See How to unzip a single file?

    Yes, zip.Reader and zip.Writer doesn't implement io.Reader and io.Writer, because they are not a single source or target of bytes. But the files in them, they are. So the files in them implement io.Reader and io.Writer. More specifically a file in an archive is represented by a zip.File which may be used to obtain an io.Reader to get its (uncompressed) content using File.Open(). When you add a new entry to a zip archive using e.g. Writer.Create(), that returns you an io.Writer because that represents a target of bytes, you can write the file's content into it.

    Back to your exmaple: MyZip also does not represent a single source or destination of bytes, so it doesn't make sense to itself implement io.Reader or io.Writer, so don't do it. Similarly to archive/zip, the individual files in it may do so.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?