C#错误 - Base-64字符串中的字符无效

时间:2011-01-20 18:16:36

标签: c#

我正在编写一个C#程序集,以便在较新版本的.NET框架中利用GZip功能,而我正在复制我在其他地方成功使用过的代码。

这种方式的工作方式是我们使用的应用程序将流提供给程序集,程序集读取它,压缩它,然后将压缩结果作为字符串返回。然后通过流将该字符串放回到程序集中以解压缩它(通常它将被存储,但我只是运行基本测试)。

但是,当我将字符串反馈给程序集时,它会从从应用程序的流中读取的字节数组中读取时出错。这是代码:

    private void ReadStream(IStream stream, out byte[] data)
    {
        using (MemoryStream writer = new MemoryStream())
        {
            IntPtr rwBytes = Marshal.AllocHGlobal(4);
            int _rwBytes = 0;
            data = new byte[0xafc8];

            do
            {
                stream.Read(data, 0xafc8, rwBytes);
                _rwBytes = Marshal.ReadInt32(rwBytes);
                writer.Write(data, 0, _rwBytes);
            }
            while (_rwBytes > 0);
            writer.Close();

            Marshal.FreeHGlobal(rwBytes);
        }
    }

    public string CompressString([In, MarshalAs(UnmanagedType.AsAny)] object appStream)
    {
        byte[] buffer = null;
        ReadStream(appStream as IStream, out buffer);

        MemoryStream ms = new MemoryStream();
        using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
        {
            zip.Write(buffer, 0, buffer.Length);
        }

        ms.Position = 0;
        MemoryStream outStream = new MemoryStream();

        byte[] compressed = new byte[ms.Length];
        ms.Read(compressed, 0, compressed.Length);

        byte[] gzBuffer = new byte[compressed.Length + 4];
        System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
        System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
        string str = Convert.ToBase64String(gzBuffer);

        gzBuffer = Convert.FromBase64String(str);
        return str;
    }

    public string DecompressString([In, MarshalAs(UnmanagedType.AsAny)] object appStream)
    {
        byte[] buffer = null;
        ReadStream(appStream as IStream, out buffer);

        string compressedText = Encoding.UTF8.GetString(buffer); 

        byte[] gzBuffer = Convert.FromBase64String(compressedText);

        using (MemoryStream ms = new MemoryStream())
        {
            int msgLength = BitConverter.ToInt32(gzBuffer, 0);
            ms.Write(gzBuffer, 4, gzBuffer.Length - 4);

            buffer = new byte[msgLength];

            ms.Position = 0;
            using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
            {
                zip.Read(buffer, 0, buffer.Length);
            }
        }

        return Encoding.UTF8.GetString(buffer);
    }

该字符串从CompressString函数返回:

yK8AAB+LCAAAAAAABADtvQdgHEmWJSYvbcp7f0r1StfgdKEIgGATJNiQQBDswYjN5pLsHWlHIymrKoHKZVZlXWYWQMztnbz33nvvvffee++997o7nU4n99//P1xmZAFs9s5K2smeIYCqyB8/fnwfPyK+ndd5WjRplk6rxarOm6aolmmbN+04TZ/n7cdN2uTU4jwt2vSqqt8241/jR8+Pnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fOj50fPj54fPT96fvT86PnR86PnR8+Pnh89P3p+9Pzo+dHz/67nR9j86PnR86PnR8+Pnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fOj50fPj54fPT96fvT86PnR86PnR8+Pnh89/99/fjSCHz0/en70/Oj50fOj50fPj54fPT96fvT86PnR86PnR8+Pnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fOj5//7z49G8KPnR8+Pnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fOj50fPj54fPT96fvT86PnR86PnR8+Pnh89P3p+9Px///nRCH70/Oj50fOj50fPj54fPT96fvT86PnR86PnR8+Pnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fOj50fPj57/7z8/GsGPnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fP/nef/AYk/UbjIrwAA

当在Encoding.UTF8.GetString调用中从流中读回它时,它是:

yK8AAB+LCAAAAAAABADtvQdgHEmWJSYvbcp7f0r1StfgdKEIgGATJNiQQBDswYjN5pLsHWlHIymrKoHKZVZlXWYWQMztnbz33nvvvffee++997o7nU4n99//P1xmZAFs9s5K2smeIYCqyB8/fnwfPyK+ndd5WjRplk6rxarOm6aolmmbN+04TZ/n7cdN2uTU4jwt2vSqqt8241/jR8+Pnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fOj50fPj54fPT96fvT86PnR86PnR8+Pnh89P3p+9Pzo+dHz/67nR9j86PnR86PnR8+Pnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fOj50fPj54fPT96fvT86PnR86PnR8+Pnh89/99/fjSCHz0/en70/Oj50fOj50fPj54fPT96fvT86PnR86PnR8+Pnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fOj5//7z49G8KPnR8+Pnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fOj50fPj54fPT96fvT86PnR86PnR8+Pnh89P3p+9Px///nRCH70/Oj50fOj50fPj54fPT96fvT86PnR86PnR8+Pnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fOj50fPj57/7z8/GsGPnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fP/nef/AYk/UbjIrwAA

这些字符串是相同的。我甚至可以在CompressString函数中来回转换它没有问题。有什么想法吗?这很奇怪。

1 个答案:

答案 0 :(得分:0)

不要使用UTF8,您应该使用ascii文本编码。当你从字符串中获取字节时,由于UTF字符串的编码方式,你最终会得到一些额外的字节。