From 55b437d8306cc8a438e772a2d2c8a09954d3e662 Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Wed, 28 Aug 2024 16:15:45 +0800 Subject: [PATCH] up --- Comic_Compressor/AvifCompressor.cs | 11 ++++----- Comic_Compressor/Program.cs | 37 ++++++++++++++++++++++++++++-- Comic_Compressor/WebpCompressor.cs | 10 ++++---- 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/Comic_Compressor/AvifCompressor.cs b/Comic_Compressor/AvifCompressor.cs index b46d270..d75a88c 100644 --- a/Comic_Compressor/AvifCompressor.cs +++ b/Comic_Compressor/AvifCompressor.cs @@ -1,6 +1,5 @@ using LibHeifSharp; using SixLabors.ImageSharp; -using SixLabors.ImageSharp.Processing; using ShellProgressBar; using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.PixelFormats; @@ -9,7 +8,7 @@ namespace Comic_Compressor { internal class AvifCompressor { - internal static void CompressImages(string sourceImagePath, string targetStoragePath) + internal static void CompressImages(string sourceImagePath, string targetStoragePath, int threadCount) { LibHeifSharpDllImportResolver.Register(); // Step 1: Get all subdirectories and store them in a list @@ -31,13 +30,13 @@ namespace Comic_Compressor foreach (string subdirectory in subdirectories) { // Step 3: Process each directory - ProcessDirectory(subdirectory, sourceImagePath, targetStoragePath, progressBar); + ProcessDirectory(subdirectory, sourceImagePath, targetStoragePath, progressBar, threadCount); } Console.WriteLine("All directories processed successfully."); } - private static void ProcessDirectory(string subdirectory, string sourceImagePath, string targetStoragePath, ShellProgressBar.ProgressBar progressBar) + private static void ProcessDirectory(string subdirectory, string sourceImagePath, string targetStoragePath, ShellProgressBar.ProgressBar progressBar, int threadCount) { // Get the relative path of the subdirectory string relativePath = Path.GetRelativePath(sourceImagePath, subdirectory); @@ -52,7 +51,7 @@ namespace Comic_Compressor // Set up ParallelOptions to limit the number of concurrent threads ParallelOptions options = new() { - MaxDegreeOfParallelism = 2 // Adjust this value to set the number of concurrent threads + MaxDegreeOfParallelism = threadCount // Adjust this value to set the number of concurrent threads }; // Process each image file in parallel @@ -82,7 +81,7 @@ namespace Comic_Compressor private static string[] GetImageFiles(string directoryPath) { // Get all image files supported by ImageSharp - string[] supportedExtensions = ["*.jpg", "*.jpeg", "*.png", "*.bmp", "*.gif", "*.tiff"]; + string[] supportedExtensions = ["*.jpg", "*.jpeg", "*.png", "*.bmp", "*.gif", "*.tiff", "*.gif"]; List allFiles = []; foreach (string extension in supportedExtensions) diff --git a/Comic_Compressor/Program.cs b/Comic_Compressor/Program.cs index bd039c4..d8fcbf4 100644 --- a/Comic_Compressor/Program.cs +++ b/Comic_Compressor/Program.cs @@ -25,6 +25,10 @@ namespace Comic_Compressor return; } + Console.WriteLine("处理线程数:"); + int threadCount = int.Parse(Console.ReadLine() ?? "2"); + Console.WriteLine($"处理线程数设定:{threadCount}"); + Console.WriteLine("请选择压缩模式:0 - 压缩成webp,1 - 压缩成avif"); string? modeInput = Console.ReadLine(); if (modeInput == null) @@ -36,10 +40,12 @@ namespace Comic_Compressor switch (modeInput) { case "0": - WebpCompressor.CompressImages(sourceImagePath, targetStoragePath); + WebpCompressor.CompressImages(sourceImagePath, targetStoragePath, threadCount); + GetCompressorResult(sourceImagePath, targetStoragePath); break; case "1": - AvifCompressor.CompressImages(sourceImagePath, targetStoragePath); + AvifCompressor.CompressImages(sourceImagePath, targetStoragePath, threadCount); + GetCompressorResult(sourceImagePath, targetStoragePath); break; default: Console.WriteLine("不支持的模式"); @@ -63,5 +69,32 @@ namespace Comic_Compressor return null; } } + + private static long GetDirectorySize(string path) + { + long size = 0; + + // 遍历目录中的所有文件 + foreach (string file in Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)) + { + // 获取文件的大小并累加到总大小中 + FileInfo fileInfo = new(file); + size += fileInfo.Length; + } + + return size; + } + + private static void GetCompressorResult(string source, string target) + { + long sourceSize = GetDirectorySize(source); + long targetSize = GetDirectorySize(target); + double reduced = (sourceSize - targetSize) * 1.0 / sourceSize; + + Console.WriteLine($"源目录大小:{sourceSize} 字节"); + Console.WriteLine($"目标目录大小:{targetSize} 字节"); + Console.WriteLine($"已减少:{reduced:P}的体积"); + } + } } diff --git a/Comic_Compressor/WebpCompressor.cs b/Comic_Compressor/WebpCompressor.cs index 7c669e2..57a0e1e 100644 --- a/Comic_Compressor/WebpCompressor.cs +++ b/Comic_Compressor/WebpCompressor.cs @@ -7,7 +7,7 @@ namespace Comic_Compressor { internal class WebpCompressor { - internal static void CompressImages(string sourceImagePath, string targetStoragePath) + internal static void CompressImages(string sourceImagePath, string targetStoragePath, int threadCount) { // Step 1: Get all subdirectories and store them in a list List subdirectories = new(Directory.GetDirectories(sourceImagePath, "*", SearchOption.AllDirectories)); @@ -28,13 +28,13 @@ namespace Comic_Compressor foreach (string subdirectory in subdirectories) { // Step 3: Process each directory - ProcessDirectory(subdirectory, sourceImagePath, targetStoragePath, progressBar); + ProcessDirectory(subdirectory, sourceImagePath, targetStoragePath, progressBar, threadCount); } Console.WriteLine("All directories processed successfully."); } - private static void ProcessDirectory(string subdirectory, string sourceImagePath, string targetStoragePath, ShellProgressBar.ProgressBar progressBar) + private static void ProcessDirectory(string subdirectory, string sourceImagePath, string targetStoragePath, ShellProgressBar.ProgressBar progressBar, int threadCount) { // Get the relative path of the subdirectory string relativePath = Path.GetRelativePath(sourceImagePath, subdirectory); @@ -49,7 +49,7 @@ namespace Comic_Compressor // Set up ParallelOptions to limit the number of concurrent threads ParallelOptions options = new() { - MaxDegreeOfParallelism = 2 // Adjust this value to set the number of concurrent threads + MaxDegreeOfParallelism = threadCount // Adjust this value to set the number of concurrent threads }; // Process each image file in parallel @@ -79,7 +79,7 @@ namespace Comic_Compressor private static string[] GetImageFiles(string directoryPath) { // Get all image files supported by ImageSharp - string[] supportedExtensions = ["*.jpg", "*.jpeg", "*.png", "*.bmp", "*.gif", "*.tiff"]; + string[] supportedExtensions = ["*.jpg", "*.jpeg", "*.png", "*.bmp", "*.gif", "*.tiff", "*.gif"]; List allFiles = []; foreach (string extension in supportedExtensions)