移除多线程支持
This commit is contained in:
parent
e403e27d22
commit
642e0a2864
@ -70,8 +70,12 @@ namespace Comic_CBZ_Helper
|
||||
List<string> comicFolders;
|
||||
if (runMode == "1") // init模式
|
||||
{
|
||||
List<string> folderOrder = new(File.ReadAllLines(listFilePath));
|
||||
comicFolders = [.. Directory.GetDirectories(parentFolder).OrderBy(folder => folderOrder.IndexOf(Path.GetFileName(folder)))];
|
||||
comicFolders = new(File.ReadAllLines(listFilePath));
|
||||
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模式或其他
|
||||
{
|
||||
@ -86,103 +90,123 @@ namespace Comic_CBZ_Helper
|
||||
DateTime currentDate = new(2020, 1, 1, 0, 0, 0);
|
||||
int count = 0;
|
||||
// 循环处理每个漫画文件夹
|
||||
Parallel.ForEach(comicFolders, new ParallelOptions { MaxDegreeOfParallelism = 4 }, comicFolder =>
|
||||
try
|
||||
{
|
||||
lock (consoleLock)
|
||||
{
|
||||
// 输出开始处理提示信息
|
||||
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)
|
||||
foreach (var comicFolder in comicFolders)
|
||||
{
|
||||
lock (consoleLock)
|
||||
{
|
||||
Console.WriteLine($"在漫画文件夹 {comicFolder} 中未找到任何图像文件。");
|
||||
// 输出开始处理提示信息
|
||||
Console.WriteLine($"开始处理漫画 {Path.GetFileName(comicFolder)}...");
|
||||
}
|
||||
return; // 继续处理下一个漫画文件夹
|
||||
}
|
||||
|
||||
// Sort the imageFiles using the NaturalSortComparer
|
||||
Array.Sort(imageFiles, new NaturalSortComparer());
|
||||
// 检查漫画文件夹是否存在
|
||||
if (!Directory.Exists(comicFolder))
|
||||
{
|
||||
lock (consoleLock)
|
||||
{
|
||||
Console.WriteLine($"漫画文件夹 {comicFolder} 不存在。");
|
||||
}
|
||||
throw new DirectoryNotFoundException($"漫画文件夹 {comicFolder} 不存在。");
|
||||
}
|
||||
|
||||
// 生成 CBZ 文件名为漫画文件夹的名称
|
||||
string cbzFileName = Path.Combine(outputFolder, $"{Path.GetFileName(comicFolder)}.cbz");
|
||||
// 确保漫画文件夹中包含图像文件
|
||||
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)
|
||||
{
|
||||
lock (consoleLock)
|
||||
{
|
||||
Console.WriteLine($"在漫画文件夹 {comicFolder} 中未找到任何图像文件。");
|
||||
}
|
||||
continue; // 继续处理下一个漫画文件夹
|
||||
}
|
||||
|
||||
// Sort the imageFiles using the NaturalSortComparer
|
||||
Array.Sort(imageFiles, new NaturalSortComparer());
|
||||
|
||||
// 生成 CBZ 文件名为漫画文件夹的名称
|
||||
string cbzFileName = Path.Combine(outputFolder, $"{Path.GetFileName(comicFolder)}.cbz");
|
||||
|
||||
// 检查 CBZ 文件是否已经存在,若存在则跳过当前漫画文件夹的处理
|
||||
if (File.Exists(cbzFileName))
|
||||
{
|
||||
lock (consoleLock)
|
||||
{
|
||||
Console.WriteLine($"CBZ 文件 {cbzFileName} 已存在,跳过。");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// 临时存储重命名后的文件路径
|
||||
string tempFolder = Path.Combine(comicFolder, "temp");
|
||||
// 检查 temp 文件夹是否存在,如果存在,则删除
|
||||
if (Directory.Exists(tempFolder))
|
||||
{
|
||||
Directory.Delete(tempFolder, true); // true 表示递归删除文件夹中的所有内容
|
||||
}
|
||||
|
||||
// 创建新的 temp 文件夹
|
||||
Directory.CreateDirectory(tempFolder);
|
||||
|
||||
// 重新编号并复制图像文件到临时文件夹
|
||||
for (int i = 0; i < imageFiles.Length; i++)
|
||||
{
|
||||
string newFileName = $"{i + 1:D4}{Path.GetExtension(imageFiles[i])}";
|
||||
string newFilePath = Path.Combine(tempFolder, newFileName);
|
||||
File.Copy(imageFiles[i], newFilePath);
|
||||
}
|
||||
|
||||
// 获取重新编号后的图像文件
|
||||
string[] renamedImageFiles = Directory.GetFiles(tempFolder);
|
||||
|
||||
// 创建 CBZ 文件
|
||||
using (FileStream zipToOpen = new(cbzFileName, FileMode.Create))
|
||||
{
|
||||
using ZipArchive archive = new(zipToOpen, ZipArchiveMode.Create);
|
||||
// 添加图像文件到 CBZ 文件
|
||||
foreach (string imageFile in renamedImageFiles)
|
||||
{
|
||||
archive.CreateEntryFromFile(imageFile, Path.GetFileName(imageFile));
|
||||
}
|
||||
string comicInfoXmlContent = runMode == "1" ? GenerateComicInfoXml2(comicFolder, currentDate) : GenerateComicInfoXml(comicFolder);
|
||||
|
||||
ZipArchiveEntry comicInfoEntry = archive.CreateEntry("ComicInfo.xml");
|
||||
using StreamWriter writer = new(comicInfoEntry.Open());
|
||||
writer.Write(comicInfoXmlContent);
|
||||
}
|
||||
|
||||
// 删除临时文件夹
|
||||
Directory.Delete(tempFolder, true);
|
||||
|
||||
// 增加日期
|
||||
lock (lockObject)
|
||||
{
|
||||
currentDate = currentDate.AddHours(7);
|
||||
count++;
|
||||
}
|
||||
|
||||
// 检查 CBZ 文件是否已经存在,若存在则跳过当前漫画文件夹的处理
|
||||
if (File.Exists(cbzFileName))
|
||||
{
|
||||
lock (consoleLock)
|
||||
{
|
||||
Console.WriteLine($"CBZ 文件 {cbzFileName} 已存在,跳过。");
|
||||
Console.WriteLine($"漫画文件夹 {comicFolder} 转换完成。");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 临时存储重命名后的文件路径
|
||||
string tempFolder = Path.Combine(comicFolder, "temp");
|
||||
// 检查 temp 文件夹是否存在,如果存在,则删除
|
||||
if (Directory.Exists(tempFolder))
|
||||
{
|
||||
Directory.Delete(tempFolder, true); // true 表示递归删除文件夹中的所有内容
|
||||
}
|
||||
|
||||
// 创建新的 temp 文件夹
|
||||
Directory.CreateDirectory(tempFolder);
|
||||
|
||||
// 重新编号并复制图像文件到临时文件夹
|
||||
for (int i = 0; i < imageFiles.Length; i++)
|
||||
{
|
||||
string newFileName = $"{i + 1:D4}{Path.GetExtension(imageFiles[i])}";
|
||||
string newFilePath = Path.Combine(tempFolder, newFileName);
|
||||
File.Copy(imageFiles[i], newFilePath);
|
||||
}
|
||||
|
||||
// 获取重新编号后的图像文件
|
||||
string[] renamedImageFiles = Directory.GetFiles(tempFolder);
|
||||
|
||||
// 创建 CBZ 文件
|
||||
using (FileStream zipToOpen = new(cbzFileName, FileMode.Create))
|
||||
{
|
||||
using ZipArchive archive = new(zipToOpen, ZipArchiveMode.Create);
|
||||
// 添加图像文件到 CBZ 文件
|
||||
foreach (string imageFile in renamedImageFiles)
|
||||
Interlocked.Increment(ref processedFolders);
|
||||
lock (consoleLock)
|
||||
{
|
||||
archive.CreateEntryFromFile(imageFile, Path.GetFileName(imageFile));
|
||||
ShowProgress(processedFolders, totalFolders);
|
||||
}
|
||||
string comicInfoXmlContent = runMode == "1" ? GenerateComicInfoXml2(comicFolder, currentDate) : GenerateComicInfoXml(comicFolder);
|
||||
|
||||
ZipArchiveEntry comicInfoEntry = archive.CreateEntry("ComicInfo.xml");
|
||||
using StreamWriter writer = new(comicInfoEntry.Open());
|
||||
writer.Write(comicInfoXmlContent);
|
||||
}
|
||||
}
|
||||
catch (DirectoryNotFoundException ex)
|
||||
{
|
||||
// 处理异常,例如输出错误信息并停止程序
|
||||
Console.WriteLine(ex.Message);
|
||||
PauseBeforeExit(0); // 停止程序,并等待用户按任意键退出
|
||||
}
|
||||
|
||||
// 删除临时文件夹
|
||||
Directory.Delete(tempFolder, true);
|
||||
|
||||
// 增加日期
|
||||
lock (lockObject)
|
||||
{
|
||||
currentDate = currentDate.AddHours(7);
|
||||
count++;
|
||||
}
|
||||
|
||||
lock (consoleLock)
|
||||
{
|
||||
Console.WriteLine($"漫画文件夹 {comicFolder} 转换完成。");
|
||||
}
|
||||
|
||||
Interlocked.Increment(ref processedFolders);
|
||||
lock (consoleLock)
|
||||
{
|
||||
ShowProgress(processedFolders, totalFolders);
|
||||
}
|
||||
});
|
||||
|
||||
PauseBeforeExit(count);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user