//--------------------文件,分割与合并---------------------------------------- using System.IO /// <summary> /// 单个文件分割函数, /// 可将任意文件fileIn分割为若干个子文件, 单个子文件最大为 len KB /// delet标识文件分割完成后是否删除原文件, change为加密密匙 /// fileIn = "D:\\file.rar", 子文件名形如"D:\\file.rar@_1.split" /// </summary> public void fileSplit(String fileIn, long KBlen, bool delet, string key) { //输入文件校验 if (fileIn == null || !System.IO.File.Exists(fileIn)) { MessageBox.Show("文件" + fileIn + "不存在!"); return; } //从文件创建输入流 FileStream FileIn = new FileStream(fileIn, FileMode.Open); byte[] data = new byte[KBlen < 1024 ? KBlen : 1024]; //流读取,缓存空间 long 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); } len += readLen; //累计读取的文件大小 curLen += readLen; //当前处理进度 if (!key.Equals("【不加密】")) { Locker(ref data, key, true); // 数据加密 } //输出,缓存数据写入子文件 FileOut.Write(data, 0, readLen); FileOut.Flush(); //预读下一轮缓存数据 readLen = FileIn.Read(data, 0, data.Length); if (len >= KBlen || readLen == 0) //子文件达到指定大小,或文件已读完 { FileOut.Close(); //关闭当前输出流 len = 0; } //显示处理进度 if (Progress != null) { Progress.Value = (int)(curLen / FileLen * Progress.Maximum); } } FileIn.Close(); //关闭输入流 if (delet) { System.IO.File.Delete(fileIn); //删除源文件 } }
/// <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) { String splitFileName = fileIn + "@_" + I++ + ".split"; outputLog("即将拆分文件" + splitFileName); FileOut = new FileStream(splitFileName, 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); //删除源文件 } }