|
| 1 | + |
| 2 | +using Microsoft.Win32.SafeHandles; |
| 3 | +using System.Collections.Concurrent; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.IO.Enumeration; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | +using Windows.Win32; |
| 8 | + |
| 9 | +namespace CompactGUI.Core; |
| 10 | + |
| 11 | +public class Compactor : ICompressor, IDisposable |
| 12 | +{ |
| 13 | + |
| 14 | + private readonly string workingDirectory; |
| 15 | + private readonly string[] excludedFileExtensions; |
| 16 | + private readonly WOFCompressionAlgorithm wofCompressionAlgorithm; |
| 17 | + |
| 18 | + |
| 19 | + private IntPtr compressionInfoPtr; |
| 20 | + private UInt32 compressionInfoSize; |
| 21 | + |
| 22 | + private long totalProcessedBytes = 0; |
| 23 | + private readonly SemaphoreSlim pauseSemaphore = new SemaphoreSlim(1, 2); |
| 24 | + private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); |
| 25 | + |
| 26 | + |
| 27 | + public Compactor(string folderPath, WOFCompressionAlgorithm compressionLevel, string[] excludedFileTypes) |
| 28 | + { |
| 29 | + workingDirectory = folderPath; |
| 30 | + excludedFileExtensions = excludedFileTypes; |
| 31 | + wofCompressionAlgorithm = compressionLevel; |
| 32 | + |
| 33 | + InitializeCompressionInfoPointer(); |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + private void InitializeCompressionInfoPointer() |
| 38 | + { |
| 39 | + var _EFInfo = new WOFHelper.WOF_FILE_COMPRESSION_INFO_V1 { Algorithm = (UInt32)wofCompressionAlgorithm, Flags = 0 }; |
| 40 | + compressionInfoPtr = Marshal.AllocHGlobal(Marshal.SizeOf(_EFInfo)); |
| 41 | + compressionInfoSize = (UInt32)Marshal.SizeOf(_EFInfo); |
| 42 | + Marshal.StructureToPtr(_EFInfo, compressionInfoPtr, true); |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + public async Task<bool> RunAsync(List<string> filesList, IProgress<CompressionProgress> progressMonitor = null, int maxParallelism = 1) |
| 47 | + { |
| 48 | + if(cancellationTokenSource.IsCancellationRequested) { return false; } |
| 49 | + |
| 50 | + var workingFiles = await BuildWorkingFilesList().ConfigureAwait(false); |
| 51 | + long totalFilesSize = workingFiles.Sum((f) => f.UncompressedSize); |
| 52 | + |
| 53 | + totalProcessedBytes = 0; |
| 54 | + |
| 55 | + if(maxParallelism <= 0) maxParallelism = Environment.ProcessorCount; |
| 56 | + ParallelOptions parallelOptions = new() { MaxDegreeOfParallelism = maxParallelism }; |
| 57 | + |
| 58 | + try |
| 59 | + { |
| 60 | + await Parallel.ForEachAsync(workingFiles, parallelOptions, |
| 61 | + (file, ctx) => |
| 62 | + { |
| 63 | + ctx.ThrowIfCancellationRequested(); |
| 64 | + return new ValueTask(PauseAndProcessFile(file, totalFilesSize, cancellationTokenSource.Token, progressMonitor)); |
| 65 | + }).ConfigureAwait(false); |
| 66 | + } |
| 67 | + catch (Exception) |
| 68 | + { |
| 69 | + |
| 70 | + return false; |
| 71 | + } |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
| 75 | + private async Task PauseAndProcessFile(FileDetails file, long totalFilesSize, CancellationToken token, IProgress<CompressionProgress> progressMonitor) |
| 76 | + { |
| 77 | + try |
| 78 | + { |
| 79 | + await pauseSemaphore.WaitAsync(token).ConfigureAwait(false); |
| 80 | + pauseSemaphore.Release(); |
| 81 | + } |
| 82 | + catch (Exception) |
| 83 | + { |
| 84 | + throw; |
| 85 | + } |
| 86 | + |
| 87 | + token.ThrowIfCancellationRequested(); |
| 88 | + |
| 89 | + var res = WOFCompressFile(file.FileName); |
| 90 | + Interlocked.Add(ref totalProcessedBytes, file.UncompressedSize); |
| 91 | + progressMonitor?.Report(new CompressionProgress((int)((double)totalProcessedBytes / totalFilesSize * 100.0), file.FileName)); |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + private unsafe int? WOFCompressFile(string filePath) |
| 96 | + { |
| 97 | + try |
| 98 | + { |
| 99 | + using (SafeFileHandle fs = File.OpenHandle(filePath)) |
| 100 | + { |
| 101 | + return PInvoke.WofSetFileDataLocation(fs, (uint)WOFHelper.WOF_PROVIDER_FILE, compressionInfoPtr.ToPointer(), compressionInfoSize); |
| 102 | + } |
| 103 | + } |
| 104 | + catch (Exception ex) |
| 105 | + { |
| 106 | + Debug.WriteLine(ex.Message); |
| 107 | + return null; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private async Task<IEnumerable<FileDetails>> BuildWorkingFilesList() |
| 112 | + { |
| 113 | + uint clusterSize = SharedMethods.GetClusterSize(workingDirectory); |
| 114 | + |
| 115 | + var filesList = new ConcurrentBag<FileDetails>(); |
| 116 | + |
| 117 | + var analyser = new Analyser(workingDirectory); |
| 118 | + var ret = await analyser.AnalyseFolder(cancellationTokenSource.Token); |
| 119 | + |
| 120 | + Parallel.ForEach(analyser.FileCompressionDetailsList, (fl) => |
| 121 | + { |
| 122 | + var ft = fl.FileInfo; |
| 123 | + if ((!excludedFileExtensions.Contains(ft?.Extension) || excludedFileExtensions.Contains(fl.FileName)) |
| 124 | + && ft.Length > clusterSize |
| 125 | + && fl.CompressionMode != wofCompressionAlgorithm) |
| 126 | + { |
| 127 | + filesList.Add(new FileDetails { FileName = fl.FileName, UncompressedSize = fl.UncompressedSize }); |
| 128 | + } |
| 129 | + }); |
| 130 | + |
| 131 | + return filesList.ToList(); |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + public void Pause() |
| 136 | + { |
| 137 | + pauseSemaphore.Wait(cancellationTokenSource.Token); |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + public void Resume() |
| 142 | + { |
| 143 | + if (pauseSemaphore.CurrentCount == 0) pauseSemaphore.Release(); |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | + public void Cancel() |
| 148 | + { |
| 149 | + Resume(); |
| 150 | + cancellationTokenSource.Cancel(); |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + public void Dispose() |
| 155 | + { |
| 156 | + cancellationTokenSource?.Dispose(); |
| 157 | + pauseSemaphore?.Dispose(); |
| 158 | + if (compressionInfoPtr != IntPtr.Zero) |
| 159 | + { |
| 160 | + Marshal.FreeHGlobal(compressionInfoPtr); |
| 161 | + compressionInfoPtr = IntPtr.Zero; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + |
| 166 | + private readonly record struct FileDetails(string FileName, long UncompressedSize); |
| 167 | + |
| 168 | + |
| 169 | +} |
0 commit comments