Compare commits

..

No commits in common. "master" and "1.0.1" have entirely different histories.

View File

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