private void InPlaceSplit(string inputFile, string outputDir, int splitNum) { var splitFileLocation = Path.Combine(outputDir, "00"); File.Move(inputFile, splitFileLocation); using (Stream input = File.Open(splitFileLocation, FileMode.Open)) { for (var i = splitNum; i >= 0; i--) { StartWritingFile?.Invoke(this, $"{i:D2}"); if (i != 0) { using (Stream output = File.Create(Path.Combine(outputDir, $"{i:D2}"))) { input.Seek(i * SplitSize, SeekOrigin.Begin); input.CopyTo(output); input.SetLength(i * SplitSize); } } FinishWritingFile?.Invoke(this, $"{i:D2}"); } } }
private void CopySplit(string inputFile, string outputDir, int splitNum) { var byteBuffer = new byte[ChunkSize]; using (Stream input = File.OpenRead(inputFile)) { for (var i = 0; i < splitNum; i++) { StartWritingFile?.Invoke(this, $"{i:D2}"); long currentPosition = 0; using (Stream output = File.Create(Path.Combine(outputDir, $"{i:D2}"))) { while (currentPosition < SplitSize && input.Position != input.Length) { currentPosition += input.Read(byteBuffer, 0, ChunkSize); output.Write(byteBuffer, 0, ChunkSize); } FinishWritingFile?.Invoke(this, $"{i:D2}"); } } if (input.Position >= input.Length) { return; } StartWritingFile?.Invoke(this, $"{splitNum:D2}"); using (Stream output = File.Create(Path.Combine(outputDir, $"{splitNum:D2}"))) { input.CopyTo(output); } FinishWritingFile?.Invoke(this, $"{splitNum:D2}"); } }