C# 文件分割和文件合并

C# 文件分割和文件合并

复制代码
        void SplitFile()
        {

            string sourceFile = "Old.mp4"; // 源文件路径
            string outputFile1 = "part1.bin"; // 第一个输出文件路径(10KB)
            string outputFile2 = "part2.bin"; // 第二个输出文件路径(剩余部分)

            int firstFileSize = 10 * 1024; // 第一个文件大小为 10KB(10 * 1024 字节)

            // 打开源文件
            using (FileStream sourceStream = File.OpenRead(sourceFile))
            {
                // 创建第一个输出文件
                using (FileStream outputStream1 = File.Create(outputFile1))
                {
                    // 读取并写入第一个文件的 10KB 数据
                    byte[] buffer = new byte[firstFileSize];
                    int bytesRead = sourceStream.Read(buffer, 0, buffer.Length);
                    outputStream1.Write(buffer, 0, bytesRead);
                }

                // 创建第二个输出文件
                using (FileStream outputStream2 = File.Create(outputFile2))
                {
                    // 读取并写入剩余的数据
                    byte[] buffer = new byte[1024]; // 1KB 缓冲区
                    int bytesRead;
                    while ((bytesRead = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        outputStream2.Write(buffer, 0, bytesRead);
                    }
                }
            }


        }

        void MergeFile()
        {

            string file1 = "part1.bin"; // 第一个二进制文件路径
            string file2 = "part2.bin"; // 第二个二进制文件路径
            string outputFile = "Merged.mp4"; // 合并后的文件路径

            // 打开输出文件流
            using (FileStream outputStream = File.Create(outputFile))
            {
                // 读取并写入第一个文件
                using (FileStream inputStream = File.OpenRead(file1))
                {
                    inputStream.CopyTo(outputStream); // 将第一个文件内容复制到输出文件
                }

                // 读取并写入第二个文件
                using (FileStream inputStream = File.OpenRead(file2))
                {
                    inputStream.CopyTo(outputStream); // 将第二个文件内容复制到输出文件
                }
            }

        }

  

相关推荐
hez20104 天前
在 .NET 上构建超大托管数组
c#·.net·.net core·gc·clr
雨落倾城夏未凉9 天前
第四章c#方法-参数数组和可选参数(16)
后端·c#
唐青枫10 天前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫11 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
咕白m62511 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net
用户917215619021111 天前
C# 通信协议增量解析:用状态机处理半包和粘包
c#
小码编匠12 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫14 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech14 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf16 天前
C#摸鱼实录——IoC与DI案例详解
c#