首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 打造命令列檔案分割與合併工具:Stream 讀寫與位元組操作實作

     編輯:關於C語言

前言

大檔案傳輸、備份常需分割成小檔案。本文實作純 C# 命令列工具 filesplitcon,支援 分割 (指定份數) 與 合併 (依清單檔),使用 FileStream/StreamReader/StreamWriter 進行高效位元組流處理,編譯後僅 6.5 KB,可部署為系統命令。

核心程式碼


using System;
using System.IO;

namespace filesplitcon
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 2) { ShowUsage(); return; }

            if (args[0] == "s") // Split
            {
                string srcFile = args[1];
                int parts = int.Parse(args[2]);

                using (FileStream fr = new FileStream(srcFile, FileMode.Open, FileAccess.Read))
                {
                    int chunkSize = (int)(fr.Length / parts);
                    using (StreamWriter list = new StreamWriter(srcFile + ".list", false))
                    {
                        for (int i = 1; i <= parts; i++)
                        {
                            byte[] buffer = new byte[chunkSize];
                            int read = fr.Read(buffer, 0, chunkSize);
                            using (FileStream fw = new FileStream($"{srcFile}.{i}", FileMode.CreateNew, FileAccess.Write))
                            {
                                fw.Write(buffer, 0, read);
                                list.WriteLine($"{srcFile}.{i}");
                            }
                        }

                        // 處理剩餘位元組
                        if (fr.Position < fr.Length)
                        {
                            int remain = (int)(fr.Length - fr.Position);
                            byte[] buffer = new byte[remain];
                            fr.Read(buffer, 0, remain);
                            using (FileStream fw = new FileStream($"{srcFile}.{parts + 1}", FileMode.CreateNew, FileAccess.Write))
                            {
                                fw.Write(buffer, 0, remain);
                                list.WriteLine($"{srcFile}.{parts + 1}");
                            }
                        }
                        list.Flush();
                    }
                }
                Console.WriteLine("檔案分割完成");
            }
            else if (args[0] == "m") // Merge
            {
                string listFile = args[1] + ".list";
                string destFile = args[1];

                using (StreamReader list = new StreamReader(listFile))
                using (FileStream fw = new FileStream(destFile, FileMode.Append, FileAccess.Write))
                {
                    string partFile;
                    while ((partFile = list.ReadLine()) != null)
                    {
                        using (FileStream fr = new FileStream(partFile, FileMode.Open, FileAccess.Read))
                        {
                            byte[] buffer = new byte[fr.Length];
                            fr.Read(buffer, 0, buffer.Length);
                            fw.Write(buffer, 0, buffer.Length);
                        }
                    }
                }
                Console.WriteLine("檔案合併完成");
            }
            else ShowUsage();
        }

        static void ShowUsage()
        {
            Console.WriteLine("用法:");
            Console.WriteLine("  filesplitcon s <檔案> <份數>   - 分割檔案");
            Console.WriteLine("  filesplitcon m <合併後檔名>   - 合併檔案 (需 .list 清單)");
        }
    }
}

操作指令範例


# 分割 w2ksp3.exe 為 4 份
filesplitcon s w2ksp3.exe 4
# 產生:w2ksp3.exe.1 ~ .4、.list

# 合併 (將 .1~.4 與 .list 放同目錄)
filesplitcon m w2ksp3.exe
# 還原 w2ksp3.exe

產出檔案說明

檔案說明
原檔名.1 ~ .N分割後的二進位區塊
原檔名.list文字清單,每行一個分割檔名,合併時依序讀取

效能與特點

  • 位元組流複製:逐區塊 Read/Write,記憶體佔用固定 (chunkSize),支援超大檔 (GB 級)。
  • 自動處理餘數:檔案大小非份數整數倍時,多產生一個餘數檔,合併時自動包含。
  • 命令列介面:無 UI 開銷,啟動快,適合批次腳本整合。
  • 單一執行檔:Release 建置後複製至 C:\Windows 或 PATH 目錄即可全域呼叫。

改進方向

  • 加入進度條、校驗碼 (MD5/SHA256) 驗證完整性。
  • 支援指定分割大小 (MB) 而非份數。
  • 使用 FileOptions.SequentialScan 優化大檔案讀取。

結語

不到 100 行代碼實作實用的檔案分割合併工具,展示 FileStream 位元組流操作核心技巧,亦為學習 C# I/O、命令列參數解析、錯誤處理的優秀練習專案。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved