mirror of
https://github.com/ShareX/ShareX.git
synced 2024-07-04 22:21:23 +12:00
Improve zip extract error message
This commit is contained in:
parent
5a932ddda8
commit
169c7c2347
|
@ -74,11 +74,12 @@ public static bool IsOddNumber(this int num)
|
||||||
|
|
||||||
public static string ToSizeString(this long size, bool binary = false, int decimalPlaces = 2)
|
public static string ToSizeString(this long size, bool binary = false, int decimalPlaces = 2)
|
||||||
{
|
{
|
||||||
if (size < 1024) return Math.Max(size, 0) + " B";
|
int bytes = binary ? 1024 : 1000;
|
||||||
int place = (int)Math.Floor(Math.Log(size, 1024));
|
if (size < bytes) return Math.Max(size, 0) + " B";
|
||||||
double num = size / Math.Pow(1024, place);
|
int place = (int)Math.Floor(Math.Log(size, bytes));
|
||||||
|
double num = size / Math.Pow(bytes, place);
|
||||||
string suffix = binary ? suffixBinary[place] : suffixDecimal[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)
|
public static string ToDecimalString(this double number, int decimalPlaces)
|
||||||
|
|
|
@ -33,16 +33,20 @@ namespace ShareX.HelpersLib
|
||||||
{
|
{
|
||||||
public static class ZipManager
|
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))
|
using (ZipArchive archive = ZipFile.OpenRead(archivePath))
|
||||||
{
|
{
|
||||||
if (maxLength > 0)
|
if (maxUncompressedSize > 0)
|
||||||
{
|
{
|
||||||
long declaredSize = archive.Entries.Sum(entry => entry.Length);
|
long totalUncompressedSize = archive.Entries.Sum(entry => entry.Length);
|
||||||
if (declaredSize > maxLength)
|
|
||||||
|
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()}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1698,7 +1698,7 @@ public static void ImportImageEffect(string filePath)
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
ex.ShowError();
|
ex.ShowError(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(configJson))
|
if (!string.IsNullOrEmpty(configJson))
|
||||||
|
|
Loading…
Reference in a new issue