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

        }

相关推荐
唐青枫4 小时前
C#.NET Channel 深入解析:高性能异步生产者消费者模型实战
c#·.net
小峥降临19 小时前
Rokid UXR 的手势追踪虚拟中更真实的手实战开发【含 工程源码 和 最终完成APK】
c#
晨星shine5 天前
GC、Dispose、Unmanaged Resource 和 Managed Resource
后端·c#
用户298698530145 天前
.NET 文档自动化:Spire.Doc 设置奇偶页页眉/页脚的最佳实践
后端·c#·.net
用户3667462526745 天前
接口文档汇总 - 2.设备状态管理
c#
用户3667462526745 天前
接口文档汇总 - 3.PLC通信管理
c#
Ray Liang6 天前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Scout-leaf9 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530149 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
mudtools10 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net