1 ///2 /// 文件分割方法 3 /// 4 /// 要分割的文件 5 /// 目标目录 6 /// 分割后的小文件名 7 public void SplitFile(string sourceFile,string desDir, string[] TempExtra) 8 { 9 int iFileSize = 9 * 1024;10 if (Directory.Exists(desDir))11 Directory.Delete(desDir, true);12 else13 Directory.CreateDirectory(desDir);14 FileStream SplitFileStream = new FileStream(sourceFile, FileMode.Open);15 BinaryReader SplitFileReader = new BinaryReader(SplitFileStream);16 byte[] TempBytes;17 int iFileCount = (int)(SplitFileStream.Length / iFileSize);18 if (SplitFileStream.Length % iFileSize != 0) iFileCount++;19 for (int i = 1; i <= iFileCount; i++)20 {21 string sTempFileName = desDir + @"\" + i.ToString().PadLeft(4, '0') + "." + TempExtra[TempExtra.Length - 1];22 23 FileStream TempStream = new FileStream(sTempFileName, FileMode.OpenOrCreate);24 BinaryWriter TempWriter = new BinaryWriter(TempStream);25 TempBytes = SplitFileReader.ReadBytes(iFileSize);26 TempWriter.Write(TempBytes);27 TempWriter.Close();28 TempStream.Close();29 }30 SplitFileReader.Close();31 SplitFileStream.Close();32 }
1 ///2 /// 文件合并 3 /// 4 /// 要合并为一个文件的目录 5 /// 要合并成的目标文件 6 public void MergeFile(string sDirectoryName, string desFile) 7 { 8 string[] arrFileNames = Directory.GetFiles(sDirectoryName); 9 int iSumFile = arrFileNames.Length;10 FileStream AddStream = new FileStream(desFile, FileMode.OpenOrCreate);11 BinaryWriter AddWriter = new BinaryWriter(AddStream);12 for (int i = 0; i < iSumFile; i++)13 {14 FileStream TempStream = new FileStream(arrFileNames[i], FileMode.Open);15 BinaryReader TempReader = new BinaryReader(TempStream);16 AddWriter.Write(TempReader.ReadBytes((int)TempStream.Length));17 TempReader.Close();18 TempStream.Close();19 }20 AddWriter.Close();21 AddStream.Close();22 }