//--------------------文件,分割与合并---------------------------------------- using System.IO /// <summary> /// 单个文件分割函数, /// 可将任意文件fileIn分割为若干个子文件, 单个子文件最大为 len KB /// delet标识文件分割完成后是否删除原文件, change为加密密匙 /// fileIn = "D:\\file.rar", 子文件名形如"D:\\file.rar@_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:\\1.rar@_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:\\1.rar@_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("文件合并完成!"); }
原文地址:http://blog.csdn.net/scimence/article/details/44827813