c# 流合并

C# 多个文件流合并

准备:

public class Fs_Chunks
    {
        public Fs_Chunks()
        { }
        
        public ObjectId _id { get; set; }
        public ObjectId files_id { get; set; }
        public int n { set; get; }
        public byte[] data { set; get; }
    }

从MongoDB中获得文件数据(有多个块数据)

List ls_data = MongoDataBLL.GetFsChunks();

第一种方法
List byteSource = new List();
for (int i = 0; i < ls_data.Count; i++)
{
    byte[] newData = ls_data[i].data;
    byteSource.AddRange(newData);
}
byte[] data = byteSource.ToArray();
Stream input = new MemoryStream(data);

第二种方法
Stream destStream = new MemoryStream();
BinaryWriter binary = new BinaryWriter(destStream);
 for (int i = 0; i < ls_data.Count; i++)
 {
     binary.Write(ls_data[i].data);
 }
 binary.Close();
 destStream.Close();

你可能感兴趣的:(C#)