Files
FileFlowsPlugins/ComicNodes/Helpers/GenericExtractor.cs
2022-08-09 17:59:22 +12:00

54 lines
2.0 KiB
C#

using SharpCompress.Archives;
using System;
using System.IO.Compression;
using System.Text.RegularExpressions;
namespace FileFlows.ComicNodes.Helpers;
internal class GenericExtractor
{
/// <summary>
/// Uncompresses a folder
/// </summary>
/// <param name="args">the node paratemers</param>
/// <param name="workingFile">the file to extract</param>
/// <param name="destinationPath">the location to extract to</param>
/// <param name="halfProgress">if the NodeParameter.PartPercentageUpdate should end at 50%</param>
internal static void Extract(NodeParameters args, string workingFile, string destinationPath, bool halfProgress = true)
{
if (args?.PartPercentageUpdate != null)
args?.PartPercentageUpdate(halfProgress ? 50 : 0);
bool isRar = workingFile.ToLowerInvariant().EndsWith(".cbr");
try
{
ArchiveFactory.WriteToDirectory(workingFile, destinationPath);
PageNameHelper.FixPageNames(destinationPath);
}
catch (Exception ex) when (isRar && ex.Message.Contains("Unknown Rar Header"))
{
UnrarCommandLine.Extract(args, workingFile, destinationPath, halfProgress: halfProgress);
}
if (args?.PartPercentageUpdate != null)
args?.PartPercentageUpdate(halfProgress ? 50 : 100);
}
internal static int GetImageCount(NodeParameters args, string workingFile)
{
bool isRar = workingFile.ToLowerInvariant().EndsWith(".cbr");
try
{
var rgxImages = new Regex(@"\.(jpeg|jpg|jpe|png|bmp|tiff|webp|gif)$", RegexOptions.IgnoreCase);
using var archive = ArchiveFactory.Open(workingFile);
var files = archive.Entries.Where(entry => !entry.IsDirectory).ToArray();
return files.Where(x => rgxImages.IsMatch(x.Key)).Count();
}
catch(Exception ex) when (isRar && ex.Message.Contains("Unknown Rar Header"))
{
return UnrarCommandLine.GetImageCount(args, workingFile);
}
}
}