使用C#进行压缩和使用javascript进行解压缩

时间:2013-10-10 11:59:38

标签: c# javascript compression gzipstream

我有像这样的json字符串

[ { "SectionDataId": "85247","Product":  "0.00"},{ "SectionDataId": "85249","Product":  "12.38"},{ "SectionDataId": "85264","Product":  "0.00"}]

我使用此论坛上的C#代码压缩了

 private string encode(string json)
    {
        byte[] buffer = Encoding.UTF8.GetBytes(json);
        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[ms.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);
    }

这个输出字符串我作为Web服务输出给出,所以我可以在javascript中使用这个字符串。但我无法使用javascript解压缩字符串。我有任何javascript库或代码来解决这个问题。

0 个答案:

没有答案