简体   繁体   English

在 C# 中使用 GzipStream.Read 不支持的解压缩方法错误

[英]Unsupported decompress method error using GzipStream.Read in c#

I get the following error: The archive entry was compressed using an unsupported compression method.我收到以下错误:存档条目是使用不受支持的压缩方法压缩的。 I got to decode the following gzip compressed base64 string, here is the string:我必须解码以下 gzip 压缩的 base64 字符串,这是字符串:

H4sIAAAAAAAAAD1SwW6bQBAdO0mDrUq9VGoPPWzVSj1ZItiO7aNjk4TI4IRgY7gtMLZxFnBhiYM/oLee+wn8QL+AT+mHVF1y6F5WM+/N07yZaQO0oBG2AaDRhGYYNH424GyS5DFvtOGE000LTjH2t1C/E2jdhgFeM7rJRPi3De3Hp5yx+SHGVIKmFsBX2R8gri96nXXf9zpduT/seArKHcWnA2WoKD30B6LuPk32mPIQsxZIHF94nmL22oYEZ0vKcoTfWNzJ7morB6s75hfapYitR5nNtd1+oMXLwptol1ok8Nur4zwcPgc3y15wuyzclZ57Nstd2ygc25VnUZ8Fk9F/rdnRL3RLlR1rc9CPi64xdY5utCjc3aLvWnrfUK63zo5FztFR3OlDT49UxbAfLvSdGc2nm65+81Dott4V8U63tsxRnK5h+eF6dTESDtpwHoTZntFCzG6WpCi9Jt9X5dDE73kojBKGz8iIIoMksldJwut5fqjKgU3ZUxh/I0lMUhrGXnLIPgvoi4DG+z0rCN+GGUnzGAlPiFdXkiQl6zxD+CRI/JAIYIN8iymhXNCRmHkc+vBWoPcYYMYpqyU/VuVlVbKZeqMa07HpkMn8UVctbSLBqUEjhE5VBn9+/SBV6ZuCS6sSw6qkcVV6XlWOEgEfxF/LI9GEw3fqC0/pmPM09HJer/OsbjQ7gXNzrBlXc7veL0j1ncGpuTBUgCa8mdKIblAcF/wDb9VytI4CAAA\= H4sIAAAAAAAAAD1SwW6bQBAdO0mDrUq9VGoPPWzVSj1ZItiO7aNjk4TI4IRgY7gtMLZxFnBhiYM​​ / oLee + wn8QL + AT + mHVF1y6F5WM + / N07yZaQO0oBG2AaDRhGYYNH424GyS5DFvtOGE000LTjH2t1C / E2jdhgFeM7rJRPi3De3Hp5yx + SHGVIKmFsBX2R8gri96nXXf9zpduT / seArKHcWnA2WoKD30B6LuPk32mPIQsxZIHF94nmL22oYEZ0vKcoTfWNzJ7morB6s75hfapYitR5nNtd1 + oMXLwptol1ok8Nur4zwcPgc3y15wuyzclZ57Nstd2ygc25VnUZ8Fk9F / rdnRL3RLlR1rc9CPi64xdY5utCjc3aLvWnrfUK63zo5FztFR3OlDT49UxbAfLvSdGc2nm65 + 81Dott4V8U63tsxRnK5h + eF6dTESDtpwHoTZntFCzG6WpCi9Jt9X5dDE73kojBKGz8iIIoMksldJwut5fqjKgU3ZUxh / I0lMUhrGXnLIPgvoi4DG + z0rCN + GGUnzGAlPiFdXkiQl6zxD + CRI / JAIYIN8iymhXNCRmHkc + vBWoPcYYMYpqyU / VuVlVbKZeqMa07HpkMn8UVctbSLBqUEjhE5VBn9 + / SBV6ZuCS6sSw6qkcVV6XlWOEgEfxF / LI9GEw3fqC0 / pmPM09HJer / OsbjQ7gXNzrBlXc7veL0j1ncGpuTBUgCa8mdKIblAcF / wDb9VytI4CAAA \\ u003d

When I first use the Convert.FromBase64String method I receive this string when I convert it into a string:当我第一次使用 Convert.FromBase64String 方法时,当我将它转换为字符串时,我会收到这个字符串:

a"\ \\b\\0\\0\\0\\0\\0\\0\\0=R n @\\;I J Tj\=l J=Y"؎ c c -0 q\pa ? \\t @ O T]r ^V3 Ӽ i\ \ \ фf\4~6 l 1o M\\vN1 P \h݆\^3 D \\r ǧ ! T \ W \ /z u :] ? x\\n \ŧ\e (= \\a >M \ \H\_x b چ\gK r X j+\\a ; \ڥ G ͵ ~ \›h Z$ ۫ <\>\\a7 ^p ,ܕ {6 ] (\ەgQ \ \ /tK \ksЏ 1u n ( ݢ Zz P ΎE Q CO TŰ\. \ͧ ~ P \ N Q a zu1\\ p\ ٞ B n ( & W y( \ Ȉ\\" $ WI y~ ʁM S\\#ILR\ ^r >\\v苀 =+\\b߆\I \\\tO WW $% <C $H \\b | ) \\Б y\ V \` ) %?V eU z \ӱ QW-m" A# NU\\~ U雂K \ê qUz^U \\\ _ #ф w \\vO 4 r^ n4; ss \Ws /H 0T & ҈nP\\ \o r \\\0\\0" a"\ \\b\\0\\0\\0\\0\\0\\0\\0=R n @\\;I J Tj\=l J=Y" c c -0 q\pa ? \\t @ O T]r ^V3 Ӽ i\ \ \ фf\4~6 l 1o M\\vN1 P \h݆\^3 D \\r ǧ ! T \ W \ /z u :] ? x\\n \ŧ\e (= \\a >M \ \H\_x b چ\gK r X j+\\a ; \ڥ G ͵ ~ \›h Z$ ۫ <\>\\a7 ^p ,ܕ {6 ] (\ەgQ \ \ /tK \ksЏ 1u n ( ݢ Zz P ΎE Q CO TŰ\. \ͧ ~ P \ N Q a zu1\\ p\ ٞ B n ( & W y( \ Ȉ\\" $ WI y~ ʁM S\\#ILR\ ^r >\\v苀 =+\\b߆\I \\\tO WW $% <C $H \\b | ) \\Б y\ V \` ) %?V eU z \ӱ QW-m" A# NU\\~ U雂K \ê qUz^U \\\ _ #ф w \\vO 4 r^ n4; ss \Ws /H 0T & ҈nP\\ \o r \\\0\\0"

could this have something to do with the problem?这可能与问题有关吗?

Here is my code:这是我的代码:

    public static string Decompress(string input)
    {
        byte[] compressed = Convert.FromBase64String(input);
        byte[] decompressed = Decompress(compressed);
        return Encoding.UTF8.GetString(decompressed);
    }

    private static byte[] Decompress(byte[] input)
    {
        using (var source = new MemoryStream(input))
        {
            byte[] lengthBytes = new byte[4];
            source.Read(lengthBytes, 0, 4);

            var length = BitConverter.ToInt32(lengthBytes, 0);
            using (var decompressionStream = new GZipStream(source,
                CompressionMode.Decompress))
            {
                var result = new byte[length];
                decompressionStream.Read(result, 0, length); Error: The archive entry was compressed using an unsupported compression method.
                return result;
            }
        }
    }

There is one little oddity in the base64 string, though it should not result in the error message you are getting. base64 字符串中有一点奇怪,尽管它不会导致您收到错误消息。 the \= should be replaced an equal sign ( = ), in order for the base64 decoding to work properly. \= 应替换为等号 ( = ),以便 base64 解码正常工作。 (I can't tell if the string actually has those five characters at the end, or if it is just a representation of a string with an equal sign at the end. In the latter case, I don't know it wouldn't just show an equal sign as opposed to a unicode escaped representation of an equal sign.) (我不知道该字符串是否真的在末尾有这五个字符,或者它是否只是一个结尾带有等号的字符串的表示。在后一种情况下,我不知道它不会只显示一个等号,而不是等号的 unicode 转义表示。)

Otherwise, that base64 string decodes to a valid gzip stream that should decompress with no problem.否则,该 base64 字符串将解码为一个有效的 gzip 流,该流应该可以毫无问题地解压缩。

I solved the issue, I use the GZipStream.CopyTo to a MemoryStream in place of the read function.我解决了这个问题,我使用 GZipStream.CopyTo 到 MemoryStream 代替读取函数。 Here is the code if anyone would need it!如果有人需要,这是代码!

   public static string Decompress(string value)
    {
        byte[] buffer = Convert.FromBase64String(value);
        byte[] decompressed;

        using (var inputStream = new MemoryStream(buffer))
        {
            using var outputStream = new MemoryStream();

            using (var gzip = new GZipStream(inputStream, CompressionMode.Decompress, leaveOpen: true))
            {
                gzip.CopyTo(outputStream);
            }

            decompressed = outputStream.ToArray();
        }

        return Encoding.UTF8.GetString(decompressed);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM