This commit is contained in:
Chenx221 2024-08-28 21:13:05 +08:00
parent 0c0d82d311
commit 9440d5a065

View File

@ -89,103 +89,91 @@ namespace Comic_CBZ_Helper
DateTime currentDate = new(2020, 1, 1, 0, 0, 0); DateTime currentDate = new(2020, 1, 1, 0, 0, 0);
int count = 0; int count = 0;
// 循环处理每个漫画文件夹 // 循环处理每个漫画文件夹
try foreach (var comicFolder in comicFolders)
{ {
foreach (var comicFolder in comicFolders) Console.WriteLine($"开始处理漫画 {Path.GetFileName(comicFolder)}...");
// 检查漫画文件夹是否存在
if (!Directory.Exists(comicFolder))
{ {
lock (consoleLock) Console.WriteLine($"漫画文件夹 {comicFolder} 不存在。");
{ return; //stop
// 输出开始处理提示信息
Console.WriteLine($"开始处理漫画 {Path.GetFileName(comicFolder)}...");
}
// 检查漫画文件夹是否存在
if (!Directory.Exists(comicFolder))
{
throw new DirectoryNotFoundException($"漫画文件夹 {comicFolder} 不存在。");
}
// 临时存储重命名后的文件路径
string tempFolder = Path.Combine(comicFolder, "temp");
// 检查 temp 文件夹是否存在,如果存在,则删除
if (Directory.Exists(tempFolder))
{
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());
// 生成 CBZ 文件名为漫画文件夹的名称
string cbzFileName = Path.Combine(outputFolder, $"{Path.GetFileName(comicFolder)}.cbz");
// 检查 CBZ 文件是否已经存在,若存在则跳过当前漫画文件夹的处理
if (File.Exists(cbzFileName))
{
Console.WriteLine($"CBZ 文件 {cbzFileName} 已存在,跳过。");
count++;
processedFolders++;
continue;
}
// 创建新的 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);
// 增加日期
currentDate = currentDate.AddHours(7);
count++;
Console.WriteLine($"漫画文件夹 {comicFolder} 转换完成。");
processedFolders++;
ShowProgress(processedFolders, totalFolders);
} }
}
catch (DirectoryNotFoundException ex) // 临时存储重命名后的文件路径
{ string tempFolder = Path.Combine(comicFolder, "temp");
// 处理异常,例如输出错误信息并停止程序 // 检查 temp 文件夹是否存在,如果存在,则删除
Console.WriteLine(ex.Message); if (Directory.Exists(tempFolder))
PauseBeforeExit(count); // 停止程序,并等待用户按任意键退出 {
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());
// 生成 CBZ 文件名为漫画文件夹的名称
string cbzFileName = Path.Combine(outputFolder, $"{Path.GetFileName(comicFolder)}.cbz");
// 检查 CBZ 文件是否已经存在,若存在则跳过当前漫画文件夹的处理
if (File.Exists(cbzFileName))
{
Console.WriteLine($"CBZ 文件 {cbzFileName} 已存在,跳过。");
count++;
processedFolders++;
continue;
}
// 创建新的 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);
// 增加日期
currentDate = currentDate.AddHours(7);
count++;
Console.WriteLine($"漫画文件夹 {comicFolder} 转换完成。");
processedFolders++;
ShowProgress(processedFolders, totalFolders);
} }