读取包含多个文件的 MemoryStream

发布于 2024-12-08 12:10:32 字数 171 浏览 0 评论 0原文

如果我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

草莓酥 2024-12-15 12:10:32

如果您不以某种方式分隔文件或不知道每个文件放入缓冲区时的确切大小,则不能直接这样做。

您可以使用压缩文件(例如 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.

鸵鸟症 2024-12-15 12:10:32

流只是一行字节。如果您将文件放在流中,您需要知道如何将它们分开。这意味着您必须知道文件的长度,或者您应该使用一些分隔符。某些(大多数)文件类型具有一种标头,但在整个流中查找这种标头也可能不是防水的,因为一个文件的标头也可能是另一个文件中的数据。

因此,如果您需要将文件写入这样的流,那么添加一些额外的信息是明智的。例如,从版本号开始,然后写入第一个文件的大小,写入文件本身,然后写入下一个文件的大小,等等......

通过从版本号开始,您可以更改这种格式。将来您可能决定还需要存储文件名。在这种情况下,您可以增加版本号,构建新格式,并且仍然能够读取之前创建的流。

如果您也存储这些流,这当然特别有用。

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.

Hello爱情风 2024-12-15 12:10:32

由于您要发送它们,因此您必须以知道如何将它们拉出的方式将它们发送到流中。最常见的方法是使用长度规范。例如,要将文件写入流:

  • 向流中写入一个整数来指示文件的数量

然后对于每个文件,

  • 写入一个整数(如果文件很大,则写入一个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:

  • write an integer to the stream to indicate the number of files

Then for each file,

  • write an integer (or a long if the files are large) to indicate the number of bytes in the file
  • write the file

To read the files back,

  • read an integer (n) to determine the number of files in the stream

Then, iterating n times,

  • read an integer (or long if that's what you chose) to determine the number of bytes in the file
  • read the file
夜深人未静 2024-12-15 12:10:32

您可以改用 IEnumerable

You could use an IEnumerable<Stream> instead.

背叛残局 2024-12-15 12:10:32

您需要自己实现这一点,您想要做的是将某种“分隔”写入流中。当您阅读时,查找分隔符,您就会知道何时遇到了新文件。

这是一个快速而肮脏的例子:

 byte[] delimiter = System.Encoding.Default.GetBytes("++MyDelimited++");

 ms.Write(myFirstFile);
 ms.Write(delimiter);
 ms.Write(mySecondFile);

 ....

 int len;
 do {
    len = ms.ReadByte(buffer, lastOffest, delimiter.Length);
    if(buffer == delimiter)
    {
       // Close and open a new file stream
    }
    // Write buffer to output stream
 } while(len > 0);

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:

 byte[] delimiter = System.Encoding.Default.GetBytes("++MyDelimited++");

 ms.Write(myFirstFile);
 ms.Write(delimiter);
 ms.Write(mySecondFile);

 ....

 int len;
 do {
    len = ms.ReadByte(buffer, lastOffest, delimiter.Length);
    if(buffer == delimiter)
    {
       // Close and open a new file stream
    }
    // Write buffer to output stream
 } while(len > 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文