Gzip压缩的结果太长C#

时间:2018-08-22 14:24:47

标签: c# compression gzip

我测试了GZip算法来压缩base64代码,但是函数的结果比原始代码长,原始长度为228412个字符,结果为241041,但是txtwizard压缩的结果为:

enter image description here

这是我的代码:

static string Compress(string text)
    {
        if (string.IsNullOrEmpty(text))
            return "";

        byte[] buffer = Encoding.UTF8.GetBytes(text);
        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);
        return Convert.ToBase64String(gzBuffer);
    }

0 个答案:

没有答案