locked
String Compress in C# and Decompress in Javascript RRS feed

  • Question

  • User-1160646140 posted

     I am Compressing string using below code in C#.

    public static string Compress(string text)
    {
        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);
    }

    But Same String I want to Decompress in Javascript.

    Can anyone help me to how decompress String in Javascript?

    Thursday, October 13, 2016 1:39 PM

Answers

  • User765422875 posted

    You can use pako which is a port of zlib.

    https://github.com/nodeca/pako

    or zlib

    https://github.com/imaya/zlib.js/blob/master/README.en.md

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 13, 2016 2:10 PM
  • User-707554951 posted

    Hi YannFreitas,
    You can solve your problem by zlib . the code as below:

    var base64Data = "eJztwTEBAAAAwqD1T20JT6AAAHgaCWAAAQ==";
    var compressData = atob(base64Data);
    var compressData = compressData.split('').map(function(e) {
        return e.charCodeAt(0);
    });
    var inflate = new Zlib.Inflate(compressData);
    var output = inflate.decompress();

    Best regards
    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 14, 2016 12:08 PM

All replies

  • User765422875 posted

    You can use pako which is a port of zlib.

    https://github.com/nodeca/pako

    or zlib

    https://github.com/imaya/zlib.js/blob/master/README.en.md

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 13, 2016 2:10 PM
  • User-707554951 posted

    Hi YannFreitas,
    You can solve your problem by zlib . the code as below:

    var base64Data = "eJztwTEBAAAAwqD1T20JT6AAAHgaCWAAAQ==";
    var compressData = atob(base64Data);
    var compressData = compressData.split('').map(function(e) {
        return e.charCodeAt(0);
    });
    var inflate = new Zlib.Inflate(compressData);
    var output = inflate.decompress();

    Best regards
    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 14, 2016 12:08 PM