public static IEnumerable <string> ShardingFileParts ( string originalFile , int singlePartSize = 1024 * 1024 , string fileExtensionNameSuffix = "x" ) { var directory = Path.GetDirectoryName(originalFile); var fileName = Path.GetFileNameWithoutExtension(originalFile); var fileExtensionName = Path.GetExtension(originalFile); fileName = Path.Combine ( directory , fileName ); var destFileNamePattern = fileName + ".{0}.{1}" + fileExtensionName + fileExtensionNameSuffix; var fileInfo = new FileInfo(originalFile); //var list = new List<string>(); var length = fileInfo.Length; if ( length > singlePartSize ) { using ( var sourceStream = File.OpenRead(originalFile) ) { var parts = Math.Ceiling((1.0d * length) / singlePartSize); var part = 0; var buffers = StreamDataHelper .ReadDataToBuffers ( sourceStream , singlePartSize ); foreach (var buffer in buffers) { part++; var destPartFileName = string .Format ( destFileNamePattern , parts , part ); //zippedPartFileName += "x"; using ( var fileStream = new FileStream ( destPartFileName , FileMode.Create , FileAccess.Write , FileShare.None ) ) { fileStream.Write(buffer, 0, buffer.Length); fileStream.Close(); //list.Add(zippedPartFileName); yield return (destPartFileName); } } } File.Delete(originalFile); //File.Delete(fileName); } else { //list.Add(zippedFileName); var destPartFileName = string.Format ( destFileNamePattern , 1 , 1 ); File.Move ( originalFile , destPartFileName ); yield return (destPartFileName); } }
private static IEnumerable <string> MergePartialFilesGroupProcess ( string mergedFileName , IOrderedEnumerable <string> orderedPartialFilesGroup , int parts ) { var list = new List <string>(); int i = 0; foreach (var file in orderedPartialFilesGroup) { i++; var fileName = Path.GetFileName(file); var fileNameSegments = fileName.Split('.'); var ii = fileNameSegments.Length - 2; int part = int.Parse(fileNameSegments[ii]); if (part == i) { using ( var sourceStream = File.OpenRead(file) ) { using ( var fileStream = ( parts > 1 ? new FileStream ( mergedFileName , FileMode.OpenOrCreate , FileAccess.Write , FileShare.None ) : new FileStream ( mergedFileName , FileMode.Create , FileAccess.Write , FileShare.None ) ) ) { if (parts > 1 && i > 1) { fileStream.Seek(0, SeekOrigin.End); } var buffers = StreamDataHelper .ReadDataToBuffers ( sourceStream , 64 * 1024 //* 1024 ); foreach (var buffer in buffers) { fileStream.Write(buffer, 0, buffer.Length); } fileStream.Close(); } sourceStream.Close(); list.Add(file); } if (i == parts) { //ExtractFileProcess(zippedFileName); yield return (mergedFileName); foreach (var x in list) { File.Delete(x); } } } else { if (File.Exists(mergedFileName)) { File.Delete(mergedFileName); } break; } } list.Clear(); }