57 lines
1.7 KiB
C#
Raw Normal View History

2022-05-14 02:58:38 +01:00
using SharpCompress.Archives;
using SharpCompress.Archives.Zip;
using SharpCompress.Common;
2022-07-09 13:08:41 -04:00
using SPT_AKI_Installer.Aki.Core.Model;
using System;
using System.IO;
2022-05-14 02:58:38 +01:00
using System.Linq;
2022-05-13 22:41:15 +01:00
2022-05-14 02:58:38 +01:00
namespace SPT_AKI_Installer.Aki.Helper
2022-05-13 22:41:15 +01:00
{
public static class ZipHelper
{
2022-07-09 13:08:41 -04:00
public static GenericResult Decompress(FileInfo ArchivePath, DirectoryInfo OutputFolderPath, IProgress<double> progress = null)
2022-05-13 22:41:15 +01:00
{
2022-07-09 13:08:41 -04:00
try
2022-05-13 22:41:15 +01:00
{
2022-07-09 13:08:41 -04:00
OutputFolderPath.Refresh();
if (!OutputFolderPath.Exists) OutputFolderPath.Create();
2022-05-14 02:58:38 +01:00
using var archive = ZipArchive.Open(ArchivePath);
2022-07-09 13:08:41 -04:00
var totalEntries = archive.Entries.Where(entry => !entry.IsDirectory);
int processedEntries = 0;
2022-05-14 02:58:38 +01:00
2022-07-09 13:08:41 -04:00
foreach (var entry in totalEntries)
2022-05-13 22:41:15 +01:00
{
2022-07-09 13:08:41 -04:00
entry.WriteToDirectory(OutputFolderPath.FullName, new ExtractionOptions()
2022-05-13 22:41:15 +01:00
{
ExtractFullPath = true,
Overwrite = true
});
2022-05-14 02:58:38 +01:00
2022-07-09 13:08:41 -04:00
processedEntries++;
if (progress != null)
{
progress.Report(Math.Floor(((double)processedEntries / totalEntries.Count()) * 100));
}
}
OutputFolderPath.Refresh();
if(!OutputFolderPath.Exists)
{
return GenericResult.FromError($"Failed to extract files: {ArchivePath.Name}");
2022-05-13 22:41:15 +01:00
}
2022-07-09 13:08:41 -04:00
return GenericResult.FromSuccess();
}
catch(Exception ex)
{
return GenericResult.FromError(ex.Message);
}
2022-05-13 22:41:15 +01:00
}
}
}