Skip to content

Commit 43fe54b

Browse files
committed
Calculate progress based on file size rather than file count. Closes #65
1 parent b44019e commit 43fe54b

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

CompactGUI.Core/Compactor.vb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,24 @@ Public Class Compactor : Implements IDisposable
2727

2828
Private _pauseSemaphore As New SemaphoreSlim(1, 2)
2929

30-
Private _processedFileCount As New Concurrent.ConcurrentDictionary(Of String, Integer)
30+
Private _processedFilesBytes As Long = 0
3131

3232
Private _cancellationTokenSource As New CancellationTokenSource
3333

34+
35+
3436
Public Async Function RunCompactAsync(Optional progressMonitor As IProgress(Of (percentageProgress As Integer, currentFile As String)) = Nothing) As Task(Of Boolean)
3537
If _cancellationTokenSource.IsCancellationRequested Then Return False
3638

3739
Dim FilesList = Await BuildWorkingFilesList()
38-
Dim totalFiles As Integer = FilesList.Count
39-
40-
_processedFileCount.Clear()
40+
Dim totalFilesSize As Long = FilesList.Sum(Function(f) f.Item2)
41+
_processedFilesBytes = 0
4142

4243

4344
Await Parallel.ForEachAsync(FilesList,
4445
Function(file, _ctx) As ValueTask
4546
If _ctx.IsCancellationRequested Then Return ValueTask.FromCanceled(_ctx)
46-
Return New ValueTask(PauseAndProcessFile(file, _cancellationTokenSource.Token, totalFiles, progressMonitor))
47+
Return New ValueTask(PauseAndProcessFile(file.Item1, _cancellationTokenSource.Token, file.Item2, totalFilesSize, progressMonitor))
4748
End Function).ConfigureAwait(False)
4849

4950

@@ -52,7 +53,7 @@ Public Class Compactor : Implements IDisposable
5253
Return True
5354
End Function
5455

55-
Private Async Function PauseAndProcessFile(file As String, _ctx As CancellationToken, totalFiles As Integer, Optional progressMonitor As IProgress(Of (percentageProgress As Integer, currentFile As String)) = Nothing) As Task
56+
Private Async Function PauseAndProcessFile(file As String, _ctx As CancellationToken, fileSize As Long, totalFilesSize As Long, Optional progressMonitor As IProgress(Of (percentageProgress As Integer, currentFile As String)) = Nothing) As Task
5657

5758
If _ctx.IsCancellationRequested Then Return
5859
Try
@@ -66,10 +67,11 @@ Public Class Compactor : Implements IDisposable
6667
If _ctx.IsCancellationRequested Then Return
6768

6869
Dim res = WOFCompressFile(file)
69-
_processedFileCount.TryAdd(file, 1)
70-
Dim incremented = _processedFileCount.Count
7170

72-
progressMonitor?.Report((CInt(((incremented / totalFiles) * 100)), file))
71+
Interlocked.Add(_processedFilesBytes, fileSize)
72+
Dim incremented = _processedFilesBytes
73+
74+
progressMonitor?.Report((CInt(((incremented / totalFilesSize) * 100)), file))
7375

7476
End Function
7577

@@ -103,18 +105,18 @@ Public Class Compactor : Implements IDisposable
103105

104106
End Function
105107

106-
Private Async Function BuildWorkingFilesList() As Task(Of IEnumerable(Of String))
108+
Private Async Function BuildWorkingFilesList() As Task(Of IEnumerable(Of (String, Long)))
107109

108110
Dim clusterSize As Integer = GetClusterSize(_workingDir)
109111

110-
Dim _filesList As New Concurrent.ConcurrentBag(Of String)
112+
Dim _filesList As New Concurrent.ConcurrentBag(Of (String, Long))
111113
'TODO: if the user has already analysed within the last minute, then skip creating a new one and use the old one
112114
Dim ax As New Analyser(_workingDir)
113115
Dim ret = Await ax.AnalyseFolder(Nothing)
114116

115117
Parallel.ForEach(ax.FileCompressionDetailsList, Sub(fl)
116118
Dim ft = fl.FileInfo
117-
If Not _excludedFileTypes.Contains(ft.Extension) AndAlso ft.Length > clusterSize AndAlso fl.CompressionMode <> _WOFCompressionLevel Then _filesList.Add(fl.FileName)
119+
If Not _excludedFileTypes.Contains(ft.Extension) AndAlso ft.Length > clusterSize AndAlso fl.CompressionMode <> _WOFCompressionLevel Then _filesList.Add((fl.FileName, fl.UncompressedSize))
118120
End Sub)
119121

120122

0 commit comments

Comments
 (0)