根据答案here我想将多个文件流写入一个流,如下所示:
为每个流的长度编号保留4个字节 每个流内容在其长度编号后写入(4字节后) 在结束流将是这样的
流= File1 len + File1 stream content + File2 len + File2 stream content + ....
示例代码:
result = new ExportResult_C()
{
PackedStudy = packed.ToArray() ,
Stream = new MemoryStream()
};
string[] zipFiles = Directory.GetFiles(zipRoot);
foreach (string fileN in zipFiles)
{
MemoryStream outFile = new MemoryStream(File.ReadAllBytes(fileN));
MemoryStream len = new MemoryStream(4);
//initiate outFile len to 4 byte push it to main stream
//Then push outFile stream to main stream
//Continue and do this for another file
}
//For test Save stream to file(s)
这是个好主意吗?我真的不知道这些评论如何成为代码行。
提前致谢。
答案 0 :(得分:1)
试试这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
byte[] testMessage = Encoding.UTF8.GetBytes("The quick brown fox jumped over the lazy dog");
MemoryStream outFile = new MemoryStream();
BinaryWriter writer = new BinaryWriter(outFile);
for (int i = 0; i < 10; i++ )
{
writer.Write(BitConverter.GetBytes(testMessage.Length), 0, 4);
writer.Write(testMessage, 0, testMessage.Length);
}
writer.Flush();
outFile.Position = 0;
BinaryReader reader = new BinaryReader(outFile, Encoding.UTF8);
while (outFile.Position < outFile.Length)
{
int size = reader.ReadInt32();
byte[] data = reader.ReadBytes(size);
}
}
}
}
答案 1 :(得分:0)