读取包含多个文件的 MemoryStream
如果我有一个 MemoryStream,我知道我向该 MemoryStream 发送了多个文件(示例 5 个文件)。是否可以从此 MemoryStream 读取数据并逐个文件地分解?
我的直觉告诉我不,因为当我们阅读时,我们正在逐字节阅读......任何帮助和可能的片段都会很棒。我在谷歌或这里找不到任何东西:(
If I have a single MemoryStream of which I know I sent multiple files (example 5 files) to this MemoryStream. Is it possible to read from this MemoryStream and be able to break apart file by file?
My gut is telling me no since when we Read, we are reading byte by byte... Any help and a possible snippet would be great. I haven't been able to find anything on google or here :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您不以某种方式分隔文件或不知道每个文件放入缓冲区时的确切大小,则不能直接这样做。
您可以使用压缩文件(例如 zip 文件)来传输多个文件。
You can't directly, not if you don't delimit the files in some way or know the exact size of each file as it was put into the buffer.
You can use a compressed file such as a zip file to transfer multiple files instead.
流只是一行字节。如果您将文件放在流中,您需要知道如何将它们分开。这意味着您必须知道文件的长度,或者您应该使用一些分隔符。某些(大多数)文件类型具有一种标头,但在整个流中查找这种标头也可能不是防水的,因为一个文件的标头也可能是另一个文件中的数据。
因此,如果您需要将文件写入这样的流,那么添加一些额外的信息是明智的。例如,从版本号开始,然后写入第一个文件的大小,写入文件本身,然后写入下一个文件的大小,等等......
通过从版本号开始,您可以更改这种格式。将来您可能决定还需要存储文件名。在这种情况下,您可以增加版本号,构建新格式,并且仍然能够读取之前创建的流。
如果您也存储这些流,这当然特别有用。
A stream is just a line of bytes. If you put the files next to each other in the stream, you need to know how to separate them. That means you must know the length of the files, or you should have used some separator. Some (most) file types have a kind of header, but looking for this in an entire stream may not be waterproof either, since the header of a file could just as well be data in another file.
So, if you need to write files to such a stream, it is wise to add some extra information. For instance, start with a version number, then, write the size of the first file, write the file itself and then write the size of the next file, etc....
By starting with a version number, you can make alterations to this format. In the future you may decide you need to store the file name as well. In that case, you can increase version number, make up a new format, and still be able to read streams that you created earlier.
This is of course especially useful if you store these streams too.
由于您要发送它们,因此您必须以知道如何将它们拉出的方式将它们发送到流中。最常见的方法是使用长度规范。例如,要将文件写入流:
然后对于每个文件,
long
)来指示文件中的字节数要读回文件,
n
) 以确定流中的文件数量然后,迭代
n
次,long
,如果是的话 的文件字节数Since you're sending them, you'll have to send them into the stream in such a way that you'll know how to pull them out. The most common way of doing this is to use a length specification. For example, to write the files to the stream:
Then for each file,
long
if the files are large) to indicate the number of bytes in the fileTo read the files back,
n
) to determine the number of files in the streamThen, iterating
n
times,long
if that's what you chose) to determine the number of bytes in the file您可以改用
IEnumerable
。You could use an
IEnumerable<Stream>
instead.您需要自己实现这一点,您想要做的是将某种“分隔”写入流中。当您阅读时,查找分隔符,您就会知道何时遇到了新文件。
这是一个快速而肮脏的例子:
You need to implement this yourself, what you would want to do is write in some sort of 'delimited' into the stream. As you're reading, look for that delimited, and you'll know when you have hit a new file.
Here's a quick and dirty example: