压缩和解压缩字符串仅产生原始字符串的第一个字母吗? [英] Compressing and decompressing a string yields only the first letter of the original string?

查看:96
本文介绍了压缩和解压缩字符串仅产生原始字符串的第一个字母吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码使用Gzip压缩字符串:

I'm compressing a string with Gzip using this code:

public static String Compress(String decompressed)
    {
        byte[] data = Encoding.Unicode.GetBytes(decompressed);
        using (var input = new MemoryStream(data))
        using (var output = new MemoryStream())
        {
            using (var gzip = new GZipStream(output, CompressionMode.Compress, true))
            {
                input.CopyTo(gzip);
            }
            return Convert.ToBase64String(output.ToArray());
        }
    }

并使用以下代码对其进行解压缩:

and decompressing it with this code:

    public static String Decompress(String compressed)
    {
        byte[] data = Convert.FromBase64String(compressed);
        using (MemoryStream input = new MemoryStream(data))
        using (GZipStream gzip = new GZipStream(input, CompressionMode.Decompress))
        using (MemoryStream output = new MemoryStream())
        {
            gzip.CopyTo(output);
            StringBuilder sb = new StringBuilder();
            foreach (byte b in output.ToArray())
                sb.Append((char)b);
            return sb.ToString();
        }
    }

在此示例代码中使用这些功能时,结果仅为字母S:

When I use these functions in this sample code, the result is only the letter S:

String test = "SELECT * FROM foods f WHERE f.name = 'chicken';";
String com = Compress(test);
String decom = Decompress(com);
Console.WriteLine(decom);

如果我调试代码,我会发现decom的值为

If I debug the code, I see that the value of decom is

S\0E\0L\0E\0C\0T\0 \0*\0 \0F\0R\0O\0M\0 \0f\0o\0o\0d\0s\0 \0f\0 \0W\0H\0E\0R\0E\0 \0f\0.\0n\0a\0m\0e\0 \0=\0 \0'\0c\0h\0i\0c\0k\0e\0n\0'\0;\0

,但显示的值仅是字母S.

but the value displayed is only the letter S.

推荐答案

以下是问题所在:

foreach (byte b in output.ToArray())
    sb.Append((char)b);

您实际上将每个字节解释为自己的字符,但实际上并非如此.相反,您需要以下行:

You are interpreting each byte as its own character, when in fact that is not the case. Instead, you need the line:

string decoded = Encoding.Unicode.GetString(output.ToArray());

根据编码将字节数组转换为字符串.

which will convert the byte array to a string, based on the encoding.

基本问题是,您正在基于编码转换为字节数组,但是在检索字节时却忽略了该编码.同样,您可能想使用Encoding.UTF8而不是Encoding.Unicode(尽管没关系,只要编码匹配即可.)

The basic problem is that you are converting to a byte array based on an encoding, but then ignoring that encoding when you retrieve the bytes. As well, you may want to use Encoding.UTF8 instead of Encoding.Unicode (though that shouldn't matter, as long as the encodings match up.)

这篇关于压缩和解压缩字符串仅产生原始字符串的第一个字母吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆