public File SplitStream(Stream stream, int partsCount, string fileName) { var file = new File(); var memoryStream = new MemoryStream(); stream.CopyTo(memoryStream); var bytesArray = memoryStream.ToArray(); file.FileParts = new List <FilePart>(); if (partsCount < bytesArray.Length) { for (int i = 0; i < partsCount; i++) { var filePart = new FilePart { Id = new Guid(), Name = fileName + i, PartNumber = i, SummaryInfo = new FileSummaryInfo { FileName = fileName, FileParts = new List <FilePartInfo>() } }; var arraySize = i == partsCount - 1 ? bytesArray.Length / partsCount : (int)Math.Round((double)bytesArray.Length / partsCount); var bytesArrayPart = new byte[arraySize]; for (int j = 0; j < arraySize; j++) { bytesArrayPart[j] = bytesArray[arraySize * i + j]; } filePart.DataBytesArray = bytesArrayPart; file.FileParts.Add(filePart); } } foreach (var filePart in file.FileParts) { filePart.SummaryInfo.FileParts = new List <FilePartInfo>(file.FileParts.Select(f => f as FilePartInfo)); } return(file); }
public Task WriteFile(File file, string folderPath) { return(Task.Run(() => { foreach (var filePart in file.FileParts) { using (var fileStream = new FileStream(folderPath + filePart.SummaryInfo.FileName + filePart.PartNumber, FileMode.OpenOrCreate, FileAccess.Write)) { using (var writer = new StreamWriter(fileStream)) { JsonSerializer serialiser = new JsonSerializer(); serialiser.Serialize(writer, filePart); } } } })); }