Compare commits

...

6 Commits

Author SHA1 Message Date
b40bd3bcf1 up 2024-08-29 10:10:54 +08:00
f3efacb784 up 2024-08-29 09:25:24 +08:00
cf900f831e fix incorrect date value 2024-08-29 00:37:15 +08:00
9440d5a065 up 2024-08-28 21:13:05 +08:00
0c0d82d311 up 2024-08-28 19:11:23 +08:00
642e0a2864 移除多线程支持 2024-08-28 18:36:10 +08:00

View File

@ -8,7 +8,6 @@ namespace Comic_CBZ_Helper
{ {
class Program class Program
{ {
private static readonly object lockObject = new();
static void Main() static void Main()
{ {
// 设置控制台的输出编码为UTF-8 // 设置控制台的输出编码为UTF-8
@ -70,8 +69,12 @@ namespace Comic_CBZ_Helper
List<string> comicFolders; List<string> comicFolders;
if (runMode == "1") // init模式 if (runMode == "1") // init模式
{ {
List<string> folderOrder = new(File.ReadAllLines(listFilePath)); comicFolders = new(File.ReadAllLines(listFilePath));
comicFolders = [.. Directory.GetDirectories(parentFolder).OrderBy(folder => folderOrder.IndexOf(Path.GetFileName(folder)))]; for (int i = 0; i < comicFolders.Count; i++)
{
comicFolders[i] = Path.Combine(parentFolder, comicFolders[i]);
}
//comicFolders = [.. Directory.GetDirectories(parentFolder).OrderBy(folder => folderOrder.IndexOf(Path.GetFileName(folder)))];
} }
else // append模式或其他 else // append模式或其他
{ {
@ -80,48 +83,47 @@ namespace Comic_CBZ_Helper
} }
int totalFolders = comicFolders.Count; int totalFolders = comicFolders.Count;
int processedFolders = 0; int processedFolders = 0;
object consoleLock = new();
// 设置初始日期 // 设置初始日期
DateTime currentDate = new(2020, 1, 1, 0, 0, 0); DateTime currentDate = new(2020, 1, 1, 0, 0, 0);
int count = 0; int count = 0;
// 循环处理每个漫画文件夹 int timeInc = 7;
Parallel.ForEach(comicFolders, new ParallelOptions { MaxDegreeOfParallelism = 4 }, comicFolder =>
// 寻找合适的日期增加量
DateTime tempDate = currentDate;
while (tempDate.AddHours(comicFolders.Count * timeInc) >= DateTime.Now)
{ {
lock (consoleLock) timeInc--;
if (timeInc == 0)
{ {
// 输出开始处理提示信息 Console.WriteLine("日期增加量无效,请修改代码");
Console.WriteLine($"开始处理漫画文件夹 {comicFolder}..."); return;
} }
tempDate = currentDate;
}
Console.WriteLine($"日期增加量为{timeInc}小时");
// 确保漫画文件夹中包含图像文件 // 循环处理每个漫画文件夹
string[] imageFiles = Directory.GetFiles(comicFolder, "*.*", SearchOption.AllDirectories) foreach (var comicFolder in comicFolders)
.Where(file => file.ToLower().EndsWith(".jpg") || file.ToLower().EndsWith(".png") || file.ToLower().EndsWith(".webp") || file.ToLower().EndsWith(".webp") || file.ToLower().EndsWith(".avif")) {
.ToArray(); // 检查漫画文件夹是否存在
if (!Directory.Exists(comicFolder))
if (imageFiles.Length == 0)
{ {
lock (consoleLock) Console.WriteLine($"漫画文件夹 {comicFolder} 不存在。");
{ return; //stop
Console.WriteLine($"在漫画文件夹 {comicFolder} 中未找到任何图像文件。");
}
return; // 继续处理下一个漫画文件夹
} }
// Sort the imageFiles using the NaturalSortComparer
Array.Sort(imageFiles, new NaturalSortComparer());
// 生成 CBZ 文件名为漫画文件夹的名称 // 生成 CBZ 文件名为漫画文件夹的名称
string cbzFileName = Path.Combine(outputFolder, $"{Path.GetFileName(comicFolder)}.cbz"); string cbzFileName = Path.Combine(outputFolder, $"{Path.GetFileName(comicFolder)}.cbz");
// 检查 CBZ 文件是否已经存在,若存在则跳过当前漫画文件夹的处理 // 检查 CBZ 文件是否已经存在,若存在则跳过当前漫画文件夹的处理
if (File.Exists(cbzFileName)) if (File.Exists(cbzFileName))
{ {
lock (consoleLock) Console.WriteLine($"CBZ 文件 {cbzFileName} 已存在,跳过。");
{ count++;
Console.WriteLine($"CBZ 文件 {cbzFileName} 已存在,跳过。"); processedFolders++;
} currentDate = currentDate.AddHours(timeInc);
return; continue;
} }
// 临时存储重命名后的文件路径 // 临时存储重命名后的文件路径
@ -132,6 +134,21 @@ namespace Comic_CBZ_Helper
Directory.Delete(tempFolder, true); // true 表示递归删除文件夹中的所有内容 Directory.Delete(tempFolder, true); // true 表示递归删除文件夹中的所有内容
} }
// 确保漫画文件夹中包含图像文件
string[] imageFiles = Directory.GetFiles(comicFolder, "*.*", SearchOption.AllDirectories)
.Where(file => file.ToLower().EndsWith(".jpg") || file.ToLower().EndsWith(".png") || file.ToLower().EndsWith(".webp") || file.ToLower().EndsWith(".avif"))
.ToArray();
if (imageFiles.Length == 0)
{
Console.WriteLine($"在漫画文件夹 {comicFolder} 中未找到任何图像文件。");
return; //stop
}
// Sort the imageFiles using the NaturalSortComparer
Array.Sort(imageFiles, new NaturalSortComparer());
// 创建新的 temp 文件夹 // 创建新的 temp 文件夹
Directory.CreateDirectory(tempFolder); Directory.CreateDirectory(tempFolder);
@ -166,23 +183,14 @@ namespace Comic_CBZ_Helper
Directory.Delete(tempFolder, true); Directory.Delete(tempFolder, true);
// 增加日期 // 增加日期
lock (lockObject) currentDate = currentDate.AddHours(timeInc);
{ count++;
currentDate = currentDate.AddHours(7); processedFolders++;
count++;
}
lock (consoleLock) Console.WriteLine($"漫画文件夹 {comicFolder} 转换完成。");
{ ShowProgress(processedFolders, totalFolders);
Console.WriteLine($"漫画文件夹 {comicFolder} 转换完成。"); }
}
Interlocked.Increment(ref processedFolders);
lock (consoleLock)
{
ShowProgress(processedFolders, totalFolders);
}
});
PauseBeforeExit(count); PauseBeforeExit(count);
} }