I have only 1GB of RAM so I cannot work with files too big with the ReadtoEnd method.
Here is the code Im trying so that the file would split in pieces of 256MB:
- Try
-
Dim BInput As New FileStream(Filename, FileMode.Open, FileAccess.Read)
-
Dim Reader As New BinaryReader(BInput)
-
-
Dim info As New FileInfo(Filename)
-
totalSize = info.Length
-
Dim i As Integer = totalSize / Dtblock 'Dtblock is a const 256mb
-
Dim completed As Integer = 0
-
-
Dim firstround As Boolean = True
-
-
If (totalSize > Dtblock) Then
-
'The file is bigger then 256MB
-
'Prepare to split
-
Dim EngineObj As New Engine()
-
ReDim buffer(Dtblock)
-
-
While completed < totalSize
-
-
Reader.Read(buffer, completed, completed + Dtblock)
-
'..
-
'here I work with the buffer, then I have to save it
-
'..
-
-
completed += Dtblock + 1
-
-
If firstround Then 'here I check if I should create or append to output
-
'Write
-
firstround = False
-
Dim BOutput As New FileStream(TxtBoxOutput.Text, FileMode.Create, FileAccess.Write)
-
Dim writer As New BinaryWriter(BOutput)
-
writer.Write(buffer)
-
Else
-
'Append
-
Dim BOutput As New FileStream(TxtBoxOutput.Text, FileMode.Append, FileAccess.Write)
-
Dim writer As New BinaryWriter(BOutput)
-
writer.Write(buffer)
-
End If
-
-
End While
-
-
Else
-
'The file is smaller then 256MB
-
-
ReDim buffer(totalSize)
-
Reader.Read(buffer, 0, totalSize)
-
..
-
'Here I work with the buffer..
-
..
-
'Write buffer to new file
-
Dim BOutput As New FileStream(TxtBoxOutput.Text, FileMode.Create, FileAccess.Write)
-
Dim writer As New BinaryWriter(BOutput)
-
writer.Write(buffer)
-
'Done, close instances of file streams
-
Reader.Close()
-
writer.Close()
-
-
End If
-
-
BInput.Dispose()
-
MessageBox.Show("Operation finished!")
-
Catch ex As Exception
-
MessageBox.Show(ex.ToString)
-
End Try
I know it might be horrible, but its the only idea I came up with.
anyone knows a better way to accomplish this?
Or if somebody find knows why I always get an exception on the "Reader.Read(buffer, completed, completed + Dtblock)" saying :
"Offset and length for the array lie outside of the valid field, or the number is greater than the number of the elements of the index to at the end of the source listing."
would be even better..
but till now I dont know why that happens because I supposed that the parameters for Read was like this:
Read(<WheretoStore>,<starting point>, <how many to read from starting point>)
correct?