123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- using ImageMagick;
- using System.IO.Compression;
- using SevenZip;
- using CompressionLevel = SevenZip.CompressionLevel;
- using CompressionMethod = SevenZip.CompressionMethod;
- using ImageMagick.Formats;
- namespace WebPnCbzMaker
- {
- internal class Program
- {
- private static void Main(string[] args)
- {
- Console.WriteLine("Hello, World!");
- SevenZipBase.SetLibraryPath(@"C:\Program Files\7-Zip\7z.dll");
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- var inputFolder = "./In";
- var outputFolder = "./Out";
-
- var inDirs = Directory.GetDirectories(inputFolder);
- for (var index = 0; index < inDirs.Length; index++)
- {
- var dir = inDirs[index];
-
- var folderName = Path.GetFileName(dir);
- Console.WriteLine($" >>> [{index + 1:0000}/{inDirs.Length:0000}] Enter folder: {folderName}");
-
- var folderNameDigits = folderName.Split('-', 2)[0];
- if (folderNameDigits.All(char.IsAsciiDigit) == false)
- {
- Console.WriteLine($"[WARN] skip folder: {folderName}");
- continue;
- }
- var cbzFileName = $"{folderNameDigits}.cbz";
- var outputPath = Path.Combine(outputFolder, folderName);
- var destCbzPath = Path.Combine(outputPath, cbzFileName);
- var cbz7MarkerPath = Path.Combine(outputPath, "WebPnCbz.7");
- if (File.Exists(destCbzPath))
- {
- if (File.Exists(cbz7MarkerPath))
- {
- Console.WriteLine($"[SKIP] skip folder, exist {cbzFileName} and 7");
- }
- else
- {
-
-
- }
- continue;
- }
- var cbzAlreadyExistPath = Path.Combine(dir, cbzFileName);
- var flagFoundExistedCbz = File.Exists(cbzAlreadyExistPath);
- var lstFilesToCbz = new List<string>();
- var lstFilesCopy = new List<string>();
- var flagSeqExist = false;
- if (flagFoundExistedCbz) lstFilesCopy.Add(cbzAlreadyExistPath);
- foreach (var filePath in Directory.GetFiles(dir))
- {
-
- if (Path.GetFileName(filePath).Equals("ComicInfo.xml", StringComparison.InvariantCultureIgnoreCase))
- {
- lstFilesToCbz.Add(filePath);
- lstFilesCopy.Add(filePath);
- continue;
- }
-
- var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
- if (fileNameWithoutExtension.Length > 0 && fileNameWithoutExtension.All(char.IsDigit))
- {
- if (flagFoundExistedCbz == false) lstFilesToCbz.Add(filePath);
- flagSeqExist = true;
- continue;
- }
-
- lstFilesCopy.Add(filePath);
- }
- if (flagSeqExist == false)
- {
- Console.WriteLine($"[WARN] skip folder: {folderName}, no SEQ file found");
- continue;
- }
-
- Directory.CreateDirectory(outputPath);
-
- foreach (var copy in lstFilesCopy)
- {
- var destFilePath = Path.Combine(outputPath, Path.GetFileName(copy));
- if (File.Exists(destFilePath))
- {
- Console.WriteLine($"[SKIP] Copy file: {Path.GetFileName(copy)}");
- continue;
- }
- Console.WriteLine($"Copy file: {Path.GetFileName(copy)}");
- File.Copy(copy, destFilePath);
- }
-
- if (flagFoundExistedCbz == false)
- {
- var dictEntries = new Dictionary<string, Stream>();
- Parallel.ForEach(lstFilesToCbz, toCbz =>
- {
- var bytes = File.ReadAllBytes(toCbz);
- try
- {
- bytes = ConvertToWebP(bytes);
- lock (dictEntries) dictEntries[Path.GetFileNameWithoutExtension(toCbz) + ".webp"] = new MemoryStream(bytes);
- Console.WriteLine($"Convert (WebP) file to archive: {Path.GetFileName(toCbz)}");
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- Console.WriteLine($"Add file to archive: {Path.GetFileName(toCbz)}");
- lock (dictEntries) dictEntries[Path.GetFileName(toCbz)] = new MemoryStream(bytes);
- }
- });
- Console.WriteLine($"Creating archive file: {destCbzPath}");
- var cbzBytes = MakeSevenZip(dictEntries);
- Console.WriteLine($"Writing archive file: {destCbzPath}");
- File.WriteAllBytes(destCbzPath, cbzBytes);
- }
- TouchFile(cbz7MarkerPath);
- Console.WriteLine($" <<< Leave folder: {folderName}");
- }
- Console.WriteLine("Bye, World!");
- }
- private static readonly WebPWriteDefines WebPDefines = new()
- {
- Lossless = false,
- Method = 6,
- };
- private static byte[] ConvertToWebP(byte[] imageBytes)
- {
- using var image = new MagickImage(imageBytes);
- using var ms = new MemoryStream();
- image.Format = MagickFormat.WebP;
- image.Write(ms, WebPDefines);
- return ms.ToArray();
- }
- private static byte[] MakeSevenZip(Dictionary<string, Stream> entries)
- {
- var szc = new SevenZipCompressor();
- szc.ArchiveFormat = OutArchiveFormat.SevenZip;
- szc.CompressionLevel = CompressionLevel.Ultra;
- szc.CompressionMethod = CompressionMethod.Lzma2;
- szc.CustomParameters["s"] = "off";
- var outStream = new MemoryStream();
- szc.CompressStreamDictionary(entries, outStream);
- return outStream.ToArray();
- }
-
-
-
-
-
-
-
-
- private static void TouchFile(string path)
- {
- try
- {
-
- if (File.Exists(path))
- {
- File.SetLastAccessTime(path, DateTime.Now);
- File.SetLastWriteTime(path, DateTime.Now);
- }
- else
- {
-
- using var _ = File.Create(path);
- }
- }
- catch (Exception ex) when (ex is UnauthorizedAccessException || ex is IOException)
- {
- throw new InvalidOperationException($"无法 touch 文件: {path}", ex);
- }
- }
- }
- }
|