From 169c7c2347330c5530fd2a3bbca8db6fcd3e7477 Mon Sep 17 00:00:00 2001 From: Jaex Date: Thu, 2 Jul 2020 05:31:39 +0300 Subject: [PATCH] Improve zip extract error message --- ShareX.HelpersLib/Extensions/NumberExtensions.cs | 9 +++++---- ShareX.HelpersLib/ZipManager.cs | 14 +++++++++----- ShareX/TaskHelpers.cs | 2 +- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/ShareX.HelpersLib/Extensions/NumberExtensions.cs b/ShareX.HelpersLib/Extensions/NumberExtensions.cs index 629499f03..da9b60d61 100644 --- a/ShareX.HelpersLib/Extensions/NumberExtensions.cs +++ b/ShareX.HelpersLib/Extensions/NumberExtensions.cs @@ -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) diff --git a/ShareX.HelpersLib/ZipManager.cs b/ShareX.HelpersLib/ZipManager.cs index 21e4c4dc6..29af26e5d 100644 --- a/ShareX.HelpersLib/ZipManager.cs +++ b/ShareX.HelpersLib/ZipManager.cs @@ -33,16 +33,20 @@ namespace ShareX.HelpersLib { public static class ZipManager { - public static void Extract(string archivePath, string destination, bool retainDirectoryStructure = true, Func filter = null, long maxLength = 0) + public static void Extract(string archivePath, string destination, bool retainDirectoryStructure = true, Func 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()}"); } } diff --git a/ShareX/TaskHelpers.cs b/ShareX/TaskHelpers.cs index d84d30727..dadc7386e 100644 --- a/ShareX/TaskHelpers.cs +++ b/ShareX/TaskHelpers.cs @@ -1698,7 +1698,7 @@ public static void ImportImageEffect(string filePath) } catch (Exception ex) { - ex.ShowError(); + ex.ShowError(false); } if (!string.IsNullOrEmpty(configJson))