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); // 将第二个文件内容复制到输出文件
                }
            }

        }

相关推荐
mudtools1 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
大飞pkz1 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
唐青枫1 天前
从入门到进阶:C#.NET Stopwatch 计时与性能测量全攻略
c#·.net
未来之窗软件服务2 天前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
1uther2 天前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
阿幸软件杂货间2 天前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
sali-tec2 天前
C# 基于halcon的视觉工作流-章34-环状测量
开发语言·图像处理·算法·计算机视觉·c#
Tiger_shl2 天前
【层面一】C#语言基础和核心语法-02(反射/委托/事件)
开发语言·c#
mudtools2 天前
.NET驾驭Word之力:COM组件二次开发全攻略之连接Word与创建你的第一个自动化文档
后端·c#
王维志2 天前
LiteDB详解
数据库·后端·mongodb·sqlite·c#·json·database