Reputation: 20464
I've tried to write a method that splits a file into smaller parts (I call those parts 'chunks'), I use a buffer of 1 MB to read/write the chunks and I'm reporting the operation progress percentage and the current chunk progress percent.
The problem is this, I have a big file of 6,74 GB and I will split it into chunks of 1 GB each one, the progress report works as expected but not for the last chunk where the progress up only to 75% 'cause of course the last part is around 750 mb and not 1 GB, then the progress only show up to 75% in the last chunk.
This is how I'm getting the percentages, the wrong progress is the ChunkProgress
value in the Else
block:
If Not ChunkIndex = ChunkCount Then
ProgressArguments =
New SplitProgressChangedArgs(
TotalProgress:=(TotalSize - SizeRemaining) * (100I / TotalSize),
ChunkProgress:=(100I / ChunkSize) * (SizeWritten - BufferLength),
ChunksToCreate:=ChunkCount + 1,
ChunksCreated:=ChunkIndex)
Else
ProgressArguments =
New SplitProgressChangedArgs(
TotalProgress:=(TotalSize - SizeRemaining) * (100I / TotalSize) - 1.0R,
ChunkProgress:=(100I / ChunkSize) * (SizeWritten - InputStream.Length),
ChunksToCreate:=ChunkCount + 1,
ChunksCreated:=ChunkIndex)
End If
TotalSize = The fixed size of the file being splitted, in bytes (in this case 6,74 gb)
ChunkSize = the fixed size of the chunk, in bytes (in this case 1 GB)
SizeRemaining = The total remaining size counter, in bytes (in this case from 6,74 gb to 0).
SizeWritten = The chunk size written counter, in bytes (in this case from 0 to 1 GB)
Someone could help me to fix the percentage when writting the last chunk?
This is the relevant code of the Class:
''' <summary>
''' Gets or sets the buffer-size to split or merge, in Bytes.
''' Default value is: 1048576 bytes (1 megabyte).
''' </summary>
''' <value>The buffer-size.</value>
Public Property BufferSize As Integer = 1048576I
''' <summary>
''' Splits the specified file.
''' </summary>
''' <param name="InputFile">Indicates the file to split.</param>
''' <param name="ChunkSize">Indicates the size of each chunk.</param>
''' <param name="ChunkName">Indicates the name-format for the chunks.</param>
''' <param name="ChunkExt">Indicates the file-extension for the chunks.</param>
''' <param name="Overwrite">
''' If set to <c>true</c> any existing file will be overwritten if needed to create a chunk,
''' otherwise, an exception will be thrown.
''' </param>
''' <param name="DeleteAfterSplit">If set to <c>true</c> the input file will be deleted after a successful split.</param>
''' <exception cref="System.OverflowException">'ChunkSize' should be smaller than the Filesize.</exception>
''' <exception cref="System.IO.IOException">File already exist</exception>
Public Sub Split(ByVal InputFile As String,
ByVal ChunkSize As Long,
Optional ByVal ChunkName As String = Nothing,
Optional ByVal ChunkExt As String = Nothing,
Optional ByVal Overwrite As Boolean = False,
Optional ByVal DeleteAfterSplit As Boolean = False)
' The progress event arguments.
Dim ProgressArguments As SplitProgressChangedArgs
' FileInfo instance of the input file.
Dim fInfo As New FileInfo(InputFile)
' The total filesize to split, in bytes.
Dim TotalSize As Long = fInfo.Length
' The remaining size to calculate the percentage, in bytes.
Dim SizeRemaining As Long = TotalSize
' Counts the length of the current chunk file to calculate the percentage, in bytes.
Dim SizeWritten As Long = 0L
' The buffer to read data and write the chunks.
Dim Buffer As Byte() = New Byte() {}
' The buffer length.
Dim BufferLength As Integer = Me.BufferSize
' The total amount of chunks to create.
Dim ChunkCount As Integer = CInt(Math.Floor(fInfo.Length / ChunkSize))
' Keeps track of the current chunk.
Dim ChunkIndex As Integer = 0I
' A zero-filled string to enumerate the chunk files.
Dim Zeros As String = String.Empty
' The given filename for each chunk.
Dim ChunkFile As String = String.Empty
' The chunk file basename.
ChunkName = If(String.IsNullOrEmpty(ChunkName),
Path.Combine(fInfo.DirectoryName, Path.GetFileNameWithoutExtension(fInfo.Name)),
Path.Combine(fInfo.DirectoryName, ChunkName))
' The chunk file extension.
ChunkExt = If(String.IsNullOrEmpty(ChunkExt),
fInfo.Extension.Substring(1I),
ChunkExt)
' If ChunkSize is bigger than filesize then...
If ChunkSize >= fInfo.Length Then
Throw New OverflowException("'ChunkSize' should be smaller than the Filesize.")
Exit Sub
' For cases where a chunksize is smaller than the buffersize.
ElseIf ChunkSize < BufferLength Then
BufferLength = CInt(ChunkSize)
End If ' ChunkSize <>...
' If not file-overwrite is allowed then...
If Not Overwrite Then
For Index As Integer = 0I To (ChunkCount)
' Set chunk filename.
Zeros = New String("0", CStr(ChunkCount).Length - CStr(Index + 1I).Length)
ChunkFile = String.Format("{0}.{1}.{2}", ChunkName, Zeros & CStr(Index + 1I), ChunkExt)
' If chunk file already exists then...
If File.Exists(ChunkFile) Then
Throw New IOException(String.Format("File already exist: {0}", ChunkFile))
Exit Sub
End If ' File.Exists(ChunkFile)
Next Index
Zeros = String.Empty
ChunkFile = String.Empty
End If ' Overwrite
' Open the file to start reading bytes.
Using InputStream As New FileStream(fInfo.FullName, FileMode.Open)
Using BinaryReader As New BinaryReader(InputStream)
While (InputStream.Position < InputStream.Length)
' Set chunk filename.
Zeros = New String("0", CStr(ChunkCount).Length - CStr(ChunkIndex + 1I).Length)
ChunkFile = String.Format("{0}.{1}.{2}", ChunkName, Zeros & CStr(ChunkIndex + 1I), ChunkExt)
' Reset written byte-length counter.
SizeWritten = 0L
' Create the chunk file to Write the bytes.
Using OutputStream As New FileStream(ChunkFile, FileMode.Create)
Using BinaryWriter As New BinaryWriter(OutputStream)
' Read until reached the end-bytes of the input file.
While (SizeWritten < ChunkSize) AndAlso (InputStream.Position < InputStream.Length)
' Read bytes from the original file (BufferSize byte-length).
Buffer = BinaryReader.ReadBytes(BufferLength)
' Write those bytes in the chunk file.
BinaryWriter.Write(Buffer)
' Increment the bytes-written counter.
SizeWritten += Buffer.Count
' Decrease the bytes-remaining counter.
SizeRemaining -= Buffer.Count
If Not ChunkIndex = ChunkCount Then
ProgressArguments =
New SplitProgressChangedArgs(
TotalProgress:=(TotalSize - SizeRemaining) * (100I / TotalSize),
ChunkProgress:=(100I / ChunkSize) * (SizeWritten - BufferLength),
ChunksToCreate:=ChunkCount + 1,
ChunksCreated:=ChunkIndex)
Else
ProgressArguments =
New SplitProgressChangedArgs(
TotalProgress:=(TotalSize - SizeRemaining) * (100I / TotalSize) - 1.0R,
ChunkProgress:=(100I / ChunkSize) * (SizeWritten - InputStream.Length),
ChunksToCreate:=ChunkCount + 1,
ChunksCreated:=ChunkIndex)
End If
' Report the progress.
RaiseEvent SplitProgressChanged(Me, ProgressArguments)
End While ' (SizeWritten < ChunkSize) AndAlso (InputStream.Position < InputStream.Length)
OutputStream.Flush()
End Using ' BinaryWriter
End Using ' OutputStream
ChunkIndex += 1I 'Increment the chunk file counter.
End While ' InputStream.Position < InputStream.Length
End Using ' BinaryReader
End Using ' InputStream
End Sub
This is an example usage:
Splitter.Split(InputFile:="C:\Test.mkv",
ChunkSize:=1073741824L,
ChunkName:="Test.Part",
ChunkExt:="mp3",
Overwrite:=True,
DeleteAfterSplit:=False)
Upvotes: 0
Views: 108
Reputation: 53600
The reason the progress doesn't show up correctly on the last chunk is because you're calculating progress against the "normal" chunk size
For example (with simplified values):
500 out of 1000 bytes with ChunkSize = 1000
500 / ChunkSize = %
500/1000 = 0.5
If the last chunk only has 750 bytes, we have to correct by dividing by (thisChunk / ChunkSize)
:
500 out of 750 bytes with ChunkSize = 1000
500 / ChunkSize / (thisChunk / ChunkSize) = %
500/1000 / (750/1000) = 0.667
Try this for the Else
case. (InputStream.Length - SizeWritten)
should get the size of the remaining chunk:
ProgressArguments =
New SplitProgressChangedArgs(
TotalProgress:=(TotalSize - SizeRemaining) * (100I / TotalSize) - 1.0R,
ChunkProgress:=(100I / ChunkSize) * (SizeWritten - InputStream.Length) / ((InputStream.Length - SizeWritten) / ChunkSize),
ChunksToCreate:=ChunkCount + 1,
ChunksCreated:=ChunkIndex)
Upvotes: 1