一个流中的多个文件,自定义流

Multiple file in one Stream, custom stream(一个流中的多个文件,自定义流)
本文介绍了一个流中的多个文件,自定义流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

根据答案此处 我想将多个文件流写入一个流,如下所示:

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

为每个流的长度编号保留 4 个字节每个流内容写在它的长度数之后(4字节之后)最后的流将是这样的

4 byte reserved for length number of each stream each stream content write after it's length number(after 4 byte) at the end stream will be something like this

Stream = File1 len + File1 流内容 + File2 len + File2 流内容 + ....

示例代码:

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)

这是个好主意吗?真的不知道注释怎么会变成代码行.

is it good idea? really don't know how that comments can be lines of code.

提前致谢.

推荐答案

试试这个

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);
            }
        }
    }
}

这篇关于一个流中的多个文件,自定义流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Is Unpivot (Not Pivot) functionality available in Linq to SQL? How?(Linq to SQL 中是否提供 Unpivot(非 Pivot)功能?如何?)
How to know if a field is numeric in Linq To SQL(如何在 Linq To SQL 中知道字段是否为数字)
Linq2SQl eager load with multiple DataLoadOptions(具有多个 DataLoadOptions 的 Linq2SQl 急切加载)
Extract sql query from LINQ expressions(从 LINQ 表达式中提取 sql 查询)
LINQ Where in collection clause(LINQ Where in collection 子句)
Orderby() not ordering numbers correctly c#(Orderby() 没有正确排序数字 c#)