diff --git a/src/libraries/System.IO.Compression/src/System/IO/Compression/ZipHelper.cs b/src/libraries/System.IO.Compression/src/System/IO/Compression/ZipHelper.cs index c4f46c1ba54f2a..22d784eb495bb3 100644 --- a/src/libraries/System.IO.Compression/src/System/IO/Compression/ZipHelper.cs +++ b/src/libraries/System.IO.Compression/src/System/IO/Compression/ZipHelper.cs @@ -22,16 +22,14 @@ internal static class ZipHelper internal static Encoding GetEncoding(string text) { - foreach (char c in text) + if (text.AsSpan().IndexOfAnyExceptInRange((char)32, (char)126) >= 0) { // The Zip Format uses code page 437 when the Unicode bit is not set. This format // is the same as ASCII for characters 32-126 but differs otherwise. If we can fit // the string into CP437 then we treat ASCII as acceptable. - if (c > 126 || c < 32) - { - return Encoding.UTF8; - } + return Encoding.UTF8; } + return Encoding.ASCII; }