World Save Zipping

- have changed the way the zip file is created. All files are now added to the zip in one pass, rather than as separate files.
This commit is contained in:
Brett Hewitson 2022-05-18 22:12:15 +10:00
parent 1c463ded1d
commit 99c4ed73fc
7 changed files with 121 additions and 184 deletions

View file

@ -108,9 +108,13 @@ namespace ServerManagerTool.Common.Utils
var selection = new List<ZipEntry>();
if (recurseFolders)
{
selection.AddRange(zip.Entries.Where(e => !e.IsDirectory && (e.FileName.StartsWith($"{sourceFolder.ToLower()}/", StringComparison.OrdinalIgnoreCase) || e.FileName.ToLower().Contains($"/{sourceFolder.ToLower()}/"))));
}
else
{
selection.AddRange(zip.Entries.Where(e => !e.IsDirectory && Path.GetDirectoryName(e.FileName).Equals(sourceFolder, StringComparison.OrdinalIgnoreCase)));
}
foreach (var entry in selection)
{
@ -140,7 +144,9 @@ namespace ServerManagerTool.Common.Utils
zip.AddFiles(filesToZip.Where(f => !string.IsNullOrWhiteSpace(f) && File.Exists(f)), preserveDirHierarchy, directoryPathInArchive);
if (!string.IsNullOrWhiteSpace(comment))
{
zip.Comment = comment;
}
zip.Save();
}
@ -200,7 +206,9 @@ namespace ServerManagerTool.Common.Utils
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.Default;
if (!string.IsNullOrWhiteSpace(comment))
{
zip.Comment = comment;
}
zip.Save(zipFile);
}
@ -212,7 +220,9 @@ namespace ServerManagerTool.Common.Utils
zip.AddFile(fileToZip, directoryPath);
if (!string.IsNullOrWhiteSpace(comment))
{
zip.Comment = comment;
}
zip.Save();
}
@ -233,7 +243,34 @@ namespace ServerManagerTool.Common.Utils
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.Default;
if (!string.IsNullOrWhiteSpace(comment))
{
zip.Comment = comment;
}
zip.Save();
}
}
public static void ZipFiles(string zipFile, Dictionary<string, List<string>> filesToZip, string comment = "")
{
if (string.IsNullOrWhiteSpace(zipFile))
throw new ArgumentNullException(nameof(zipFile));
if (filesToZip is null || filesToZip.IsEmpty())
throw new ArgumentNullException(nameof(filesToZip));
using (var zip = new ZipFile(zipFile))
{
foreach (var zipFolder in filesToZip.Keys)
{
zip.AddFiles(filesToZip[zipFolder].Where(f => !string.IsNullOrWhiteSpace(f) && File.Exists(f)), false, zipFolder);
}
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.Default;
if (!string.IsNullOrWhiteSpace(comment))
{
zip.Comment = comment;
}
zip.Save();
}