53 lines
1.5 KiB
C#
Raw Normal View History

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