標籤:c# 檔案 分割 合并
//--------------------檔案,分割與合并---------------------------------------- using System.IO /// <summary> /// 單個檔案分割函數, /// 可將任意檔案fileIn分割為若干個子檔案, 單個子檔案最大為 len KB /// delet標識檔案分割完成後是否刪除原檔案, change為加密密匙 /// fileIn = "D:\\file.rar", 子檔案名稱形如"D:\\[email protected]_1.split" /// </summary> public void fileSplit(String fileIn, int KBlen, bool delet, int change) { //輸入檔案校正 if (fileIn == null || !System.IO.File.Exists(fileIn)) { MessageBox.Show("檔案" + fileIn + "不存在!"); return; } //加密初始化 short sign = 1; int num = 0, tmp; if (change < 0) { sign = -1; change = -change; } //取檔案名稱和尾碼, fileIn = "D:\\1.rar" //從檔案建立輸入資料流 FileStream FileIn = new FileStream(fileIn, FileMode.Open); byte[] data = new byte[1024]; //流讀取,緩衝空間 int len = 0, I = 1; //記錄子檔案累積讀取的KB大小, 分割的子檔案序號 FileStream FileOut = null; //輸出資料流 int readLen = 0; //每次實際讀取的位元組大小 while (readLen > 0 || (readLen = FileIn.Read(data, 0, data.Length)) > 0) //讀取資料 { //建立分割後的子檔案,已有則覆蓋,子檔案"D:\\[email protected]_1.split" if (len == 0) FileOut = new FileStream(fileIn + "@_" + I++ + ".split", FileMode.Create); //加密邏輯,對data的首位元組進行邏輯位移加密 if (num == 0) num = change; tmp = data[0] + sign * (num % 3 + 3); if (tmp > 255) tmp -= 255; else if (tmp < 0) tmp += 255; data[0] = (byte)tmp; num /= 3; //輸出,快取資料寫入子檔案 FileOut.Write(data, 0, readLen); FileOut.Flush(); //預讀下一輪快取資料 readLen = FileIn.Read(data, 0, data.Length); if (++len >= KBlen || readLen == 0) //子檔案達到指定大小,或檔案已讀完 { FileOut.Close(); //關閉當前輸出資料流 len = 0; } } FileIn.Close(); //關閉輸入資料流 if (delet) System.IO.File.Delete(fileIn); //刪除源檔案 }
/// <summary> /// 對多個檔案進行分割操作 /// </summary> public void fileSplit(String[] fileIn, int KBlen, bool delet, int change) { if (fileIn == null || fileIn.Length == 0) return; for (int i = 0; i < fileIn.Length; i++) fileSplit(fileIn[i], KBlen, delet, change); MessageBox.Show("檔案分割完成!"); }
/// <summary> /// 檔案合并函數, /// 可將任意個子檔案合并為一個,為fileSplit()的逆過程 /// delet標識是否刪除原檔案, change對data的首位元組進行解密 /// </summary> public void fileCombine(String[] fileIn, bool delet, int change) { //輸入檔案名稱校正 if (fileIn == null || fileIn.Length == 0) return; //加密初始化 short sign = 1; int num = 0, tmp; if (change < 0) { sign = -1; change = -change; } //從首個子檔案解析原檔案名稱, fileIn[0] = "D:\\[email protected]_1.split" string name = fileIn[0]; int i1 = name.LastIndexOf("@_"), i2 = name.LastIndexOf('.'); name = name.Substring(0, i2); //剔除子檔案拓展名".split" string fileOut = (i1 == -1) ? name : name.Remove(i1, i2 - i1); //剔除"@_1" -> "D:\\1.rar.split" //建立輸出檔案,已有則覆蓋 FileStream FileOut = new FileStream(fileOut, FileMode.Create); for (int i = 0; i < fileIn.Length; i++) { //輸入檔案校正 if (fileIn[i] == null || !System.IO.File.Exists(fileIn[i])) continue; //從子檔案建立輸入資料流 FileStream FileIn = new FileStream(fileIn[i], FileMode.Open); byte[] data = new byte[1024]; //流讀取,緩衝空間 int readLen = 0; //每次實際讀取的位元組大小 while ((readLen = FileIn.Read(data, 0, data.Length)) > 0) //讀取資料 { //解密邏輯,對data的首位元組進行邏輯位移解密 if (num == 0) num = change; tmp = data[0] + sign * (num % 3 + 3); if (tmp > 255) tmp -= 255; else if (tmp < 0) tmp += 255; data[0] = (byte)tmp; num /= 3; //輸出,快取資料寫入檔案 FileOut.Write(data, 0, readLen); FileOut.Flush(); } //關閉輸入資料流,刪除源檔案 FileIn.Close(); if (delet) System.IO.File.Delete(fileIn[i]); } FileOut.Close(); //關閉輸出資料流 }
/// <summary> /// 對多組子檔案進行合并 /// </summary> public void fileCombine(String[][] fileIn, bool delet, int change) { for (int i = 0; i < fileIn.Length; i++) fileCombine(fileIn[i], delet, change); MessageBox.Show("檔案合并完成!"); }
C#檔案的分割與合并