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!

图片转代码服务由CSDN问答提供 功能建议

有关zip和 io.Reader / io.Writer 。 据我了解, io / Reader / io.Writer 的目的之一就是流式传输。 但是,如果我的类型对“像块一样”没有真正意义,我应该实现其中之一吗?



 类型MyZip struct {
 file1,file2 [] byte 

< code> MyZip 代表特定的结构化zip。 例如,假设它表示一个zip文件,该文件正好包含一个名为 file1 的文件和一个名为 file2 的文件。 MyZip 负责解析一个zip文件,以将这两个文件提取到两个 [] byte 字段中。 它还应该处理另一种方式(将这两个 [] byte 字段转换为两个文件,分别名为 test1 test2 的文件存档到zip文件中)

据我了解,软件包 archive / zip 不允许将zip文件解压缩为流。 我们必须将zip文件完全加载到内存中或作为文件加载,然后再解压缩。

因此,为简化我的问题, MyZip 实现< 可以读取/写入最终zip文件的code> io.Reader / io.Writer 吗?

如上所述,因为我不能 快速提取两个文件,我必须在 MyZip 中添加某种缓冲区,然后从该缓冲区读取/写入。 因此,无论如何,zip都会在流式传输之前完全存储在内存中。 是不使用 io.Reader / io.Writer 的反指示吗?

非常感谢您的光临! p>

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题