using System;
using System.IO.Compression;
namespace FileFlows.ComicNodes.Helpers;
internal class ZipHelper
{
///
/// Zips a folder to a file
///
/// the NodeParameters
/// the directory to zip
/// the output file of the zip
/// the file pattern to include in the zip
/// If all directories should be included or just the top most
/// if the NodePArameter.PartPercentageUpdate should start at 50%
internal static void Compress(NodeParameters args, string directory, string output, string pattern = "*.*", bool allDirectories = false, bool halfProgress = true)
{
var dir = new DirectoryInfo(directory);
var files = dir.GetFiles(pattern, allDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
using FileStream fs = new FileStream(output, FileMode.Create);
using ZipArchive arch = new ZipArchive(fs, ZipArchiveMode.Create);
if(args?.PartPercentageUpdate != null)
args?.PartPercentageUpdate(halfProgress ? 50 : 0);
float current = 0;
float count = files.Length;
foreach (var file in files)
{
++count;
string relative = file.FullName.Substring(dir.FullName.Length + 1);
try
{
arch.CreateEntryFromFile(file.FullName, relative, CompressionLevel.SmallestSize);
}
catch (Exception ex)
{
args.Logger?.WLog("Failed to add file to zip: " + file.FullName + " => " + ex.Message);
}
if (args?.PartPercentageUpdate != null)
{
float percent = (current / count) * 100f;
if (halfProgress)
percent = 50 + (percent / 2);
args?.PartPercentageUpdate(percent);
}
}
if (args?.PartPercentageUpdate != null)
args?.PartPercentageUpdate(100);
}
internal static void Extract(NodeParameters args, string workingFile, string destinationPath, bool halfProgress = true)
{
if (args?.PartPercentageUpdate != null)
args?.PartPercentageUpdate(halfProgress ? 50 : 0);
ZipFile.ExtractToDirectory(workingFile, destinationPath);
PageNameHelper.FixPageNames(destinationPath);
if (args?.PartPercentageUpdate != null)
args?.PartPercentageUpdate(halfProgress ? 50 : 100);
}
}