Multiple file in one Stream, custom stream

后端 未结 2 1237
野性不改
野性不改 2020-12-10 22:58

According to the answer here I want to write multiple files stream to one stream as following:

4 byte reserved for length number of each stream each stream content w

相关标签:
2条回答
  • 2020-12-10 23:45

    I think there is a better solution I posted as answer to my question here multiple file byte will be serialized to one stream and in client side it will be deserialized to a class of byte array.

    see here, it may be useful.

    But I have accepted the @jdweng solution and I appreciate his attention and help.

    0 讨论(0)
  • 2020-12-10 23:47

    Try this

    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);
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题