Improve zip extract error message

This commit is contained in:
Jaex 2020-07-02 05:31:39 +03:00
parent 5a932ddda8
commit 169c7c2347
3 changed files with 15 additions and 10 deletions

View file

@ -74,11 +74,12 @@ public static bool IsOddNumber(this int num)
public static string ToSizeString(this long size, bool binary = false, int decimalPlaces = 2)
{
if (size < 1024) return Math.Max(size, 0) + " B";
int place = (int)Math.Floor(Math.Log(size, 1024));
double num = size / Math.Pow(1024, place);
int bytes = binary ? 1024 : 1000;
if (size < bytes) return Math.Max(size, 0) + " B";
int place = (int)Math.Floor(Math.Log(size, bytes));
double num = size / Math.Pow(bytes, place);
string suffix = binary ? suffixBinary[place] : suffixDecimal[place];
return string.Format("{0} {1}", num.ToDecimalString(decimalPlaces.Clamp(0, 3)), suffix);
return num.ToDecimalString(decimalPlaces.Clamp(0, 3)) + " " + suffix;
}
public static string ToDecimalString(this double number, int decimalPlaces)

View file

@ -33,16 +33,20 @@ namespace ShareX.HelpersLib
{
public static class ZipManager
{
public static void Extract(string archivePath, string destination, bool retainDirectoryStructure = true, Func<ZipArchiveEntry, bool> filter = null, long maxLength = 0)
public static void Extract(string archivePath, string destination, bool retainDirectoryStructure = true, Func<ZipArchiveEntry, bool> filter = null,
long maxUncompressedSize = 0)
{
using (ZipArchive archive = ZipFile.OpenRead(archivePath))
{
if (maxLength > 0)
if (maxUncompressedSize > 0)
{
long declaredSize = archive.Entries.Sum(entry => entry.Length);
if (declaredSize > maxLength)
long totalUncompressedSize = archive.Entries.Sum(entry => entry.Length);
if (totalUncompressedSize > maxUncompressedSize)
{
throw new Exception("Archive is too big.");
throw new Exception("Uncompressed file size of this archive is bigger than the maximum allowed file size.\r\n\r\n" +
$"Archive uncompressed file size: {totalUncompressedSize.ToSizeString()}\r\n" +
$"Maximum allowed file size: {maxUncompressedSize.ToSizeString()}");
}
}

View file

@ -1698,7 +1698,7 @@ public static void ImportImageEffect(string filePath)
}
catch (Exception ex)
{
ex.ShowError();
ex.ShowError(false);
}
if (!string.IsNullOrEmpty(configJson))