// 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); // } // // /// // /// Checks if a file exist in the archive // /// // /// the path to the archive // /// the file to look for // /// true if exists, otherwise false // public static Result FileExists(string archivePath, string file) // { // // Check if the archive file exists // if (File.Exists(archivePath) == false) // return Result.Fail("Archive file not found: " + archivePath); // // try // { // // Open the zip archive // using ZipArchive archive = ZipFile.OpenRead(archivePath); // return archive.Entries.Any(x => x.FullName.ToLowerInvariant().Equals(file.ToLowerInvariant())); // } // catch (Exception ex) // { // return Result.Fail(ex.Message); // } // } // // /// // /// Adds a file to the archive // /// // /// the path to the archive // /// the file to add // /// true if successfully added // public static Result AddToArchive(string archivePath, string file) // { // // Check if the archive file exists // if (File.Exists(archivePath) == false) // return Result.Fail("Archive file not found: " + archivePath); // // try // { // // Open the zip archive // using ZipArchive archive = ZipFile.Open(archivePath, ZipArchiveMode.Update); // // // Create a new entry for the file // archive.CreateEntryFromFile(file, Path.GetFileName(file)); // // // Successfully added the file // return Result.Success(true); // } // catch (Exception ex) // { // return Result.Fail(ex.Message); // } // } // }