Day Helpers
net8.0 Wrap in try Zip with directories
This commit is contained in:
@ -270,12 +270,30 @@ internal static partial class HelperZipFilesBy
|
||||
private static void ZipDirectory(ILogger<Worker> logger, string directory)
|
||||
{
|
||||
logger.LogInformation("{directory}", directory);
|
||||
string zipFile = $"{directory}.zip";
|
||||
int skipChars = directory.Length + 1;
|
||||
string[] files = Directory.GetFiles(directory, "*", SearchOption.AllDirectories);
|
||||
using ZipArchive zip = ZipFile.Open($"{directory}.zip", ZipArchiveMode.Create);
|
||||
foreach (string file in files)
|
||||
string[] directories = Directory.GetDirectories(directory, "*", SearchOption.AllDirectories);
|
||||
ZipArchiveMode zipArchiveMode = File.Exists(zipFile) ? ZipArchiveMode.Update : ZipArchiveMode.Create;
|
||||
for (int i = 1; i < 3; i++)
|
||||
{
|
||||
_ = zip.CreateEntryFromFile(file, Path.GetFileName(file));
|
||||
File.Delete(file);
|
||||
try
|
||||
{
|
||||
using ZipArchive zip = ZipFile.Open(zipFile, zipArchiveMode);
|
||||
for (int j = 0; j < directories.Length; j++)
|
||||
_ = zip.CreateEntry($"{directories[j][skipChars..]}/");
|
||||
foreach (string file in files)
|
||||
{
|
||||
_ = zip.CreateEntryFromFile(file, file[skipChars..]);
|
||||
File.Delete(file);
|
||||
}
|
||||
break;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
File.Delete(zipFile);
|
||||
zipArchiveMode = ZipArchiveMode.Create;
|
||||
}
|
||||
}
|
||||
Directory.Delete(directory, recursive: true);
|
||||
}
|
||||
|
Reference in New Issue
Block a user