We had a process that split large files into smaller files be reading and writing line by line. This fixed the out of memory exceptions we were having, but it took hours to complete. I rewrote this method, so it is way faster, and doesn't cause memory exceptions.
vb Code:
Private Function SplitFile( _ ByVal inputFileName As String, ByVal outputFileName As String, ByVal numberOfFiles As Integer) _ As List(Of String) Dim returnList As New List(Of String) Try Dim outputFileExtension As String = IO.Path.GetExtension(outputFileName) outputFileName = outputFileName.Replace(outputFileExtension, "") Dim sr As New IO.StreamReader(inputFileName) Dim fileLength As Long = sr.BaseStream.Length Dim baseBufferSize As Integer = CInt(fileLength \ numberOfFiles) Dim finished As Boolean = False Dim fileCount As Integer = 1 Do Until finished Dim bufferSize As Integer = baseBufferSize Dim originalPosition As Long = sr.BaseStream.Position 'find line first line feed after the base buffer length sr.BaseStream.Position += bufferSize If sr.BaseStream.Position < fileLength Then Do Until sr.Read = 10 bufferSize += 1 Loop bufferSize += 1 Else bufferSize = CInt(fileLength - originalPosition) finished = True End If 'write the chunk of data to a buffer in memory sr.BaseStream.Position = originalPosition Dim buffer(bufferSize - 1) As Byte sr.BaseStream.Read(buffer, 0, bufferSize) 'write the chunk of data to a file Dim outputPath As String = outputFileName & fileCount.ToString & outputFileExtension returnList.Add(outputPath) My.Computer.FileSystem.WriteAllBytes( _ outputPath, buffer, False) fileCount += 1 Loop Catch ex As Exception Console.Write(ex.ToString) End Try Return returnList End Function




Reply With Quote