3

I tried to deflate a string I got from a websocket connection, but everytime I try to run this code I get an Error. I also tried to get the bytes directly from the string with an ascii encoding but it didn't work either.

public static byte[] Decompress(string input)
{

    byte[] data = Convert.FromBase64String(input)
    byte[] decompressedArray = null;

    using (MemoryStream decompressedStream = new MemoryStream())
    {
        using (MemoryStream compressStream = new MemoryStream(data))
        {
            using (DeflateStream deflateStream = new DeflateStream(compressStream, CompressionMode.Decompress))
            {
                deflateStream.CopyTo(decompressedStream);
            }
        }
        decompressedArray = decompressedStream.ToArray();
    }
    return decompressedArray;
}

System.IO.InvalidDataException: "Block length does not match with its complement."

I tried a few diffrent variations of this code but nothing worked, but i know that its possible because I can deflate it with this website http://www.txtwizard.net/compression and the deflate option.

One example string I want to deflate:

eAGdVMmO2kAU/BXUc3WYXr1wy3LIKYqUjHKIo6jtboODsVG72YT491SbIWMYkcNgiX4uv6pXr7efxHXdatYvtDPs8Qf7wkh0JF3xx5a+J7MjUVnKK8tsQbOEJqyQ2pQ0kSJ8s6118wOZcSlpROp2q411n7Xb2t5bAzyO5SkiymjDdFLaSgpZJCZOK6NYTINE7ztnx1pMifg0kAwrK16mqTSWSl4VcWGVGkiLOniTjKJqa/f+ky314Xu9ghBXIpFZSoPEXK/sCyqylAWTVRfKrTqDbLLrXGMIcrd1v9ENkGNOfE5mOXE5iXKyRywSBAcEFOMOI+MIFghijD1GkKq6aRDlpGh0uRy4vXfd0g7gDpYRIb1b67L2g9qUq9Mpb/9VRF0keDQ0cD5+fZo89Xp+5gUjUuB7oDKMl7pdG9LpNL3Sas7uGT6JZEpVIIYXNg3hniOU6TQb8PAC/H9WNs7Z1t/YgfDAB50P0bMl3dTzFmBOGlvB3JWxmyb5VIrJ40Se7QZX8Vh07OleS2FSXjckxtSbou+31mFe77cjIfmmZlLMZbBz3Ygce7nXhryzMsDH9JtWPmzKpfWTbzhG450yWhr1tqXJsix93QnExmZGvcgwZ5ct9rLHBjRsMIXNBiqJyKa37vlqUYZJQ1NDVZEmlRA6sfgL5/N3jfsDl8+djLNKiwMeDrGvnG6XkC60mQ+3iT+sw2XAI1J2TecYsh60KbQtkDVAPEA8VpXJLhDuNPLAdHgArbXTKzJ7F4uIVE29JjPvNvaE36+/hpSDkQ==

Jimi
  • 29,621
  • 8
  • 43
  • 61

1 Answers1

3

The string (JSON) probably comes from a GZipStream.
You can use this other stream to decompress the data content:

► You need .Net Framework 4.7.2+ to decompress the data correctly.

As a note, the compressed format is a little off. The compressed data signature, 78 : 01, may suggest that this is a ZLib compressed string, so the .Net DeflateStream, which is based on ZLib (from .NET Framework 4.5), should decompress the data.
Both 7-Zip and WinZip cannot open the data (a stream saved as a binary file).

using System.IO;
using System.IO.Compression;
using System.Text;

public static byte[] Decompress(string input)
{
    byte[] data = Convert.FromBase64String(input)

    using (var decompressedStream = new MemoryStream())
    using (var compressStream = new MemoryStream(data))
    using (var gzip = new GZipStream(compressStream, CompressionMode.Decompress)) {
        gzip.CopyTo(decompressedStream);
        return decompressedStream.ToArray();
    }
}

The JSON is UTF8-Encoded (it also applies to an ASCII string):

string json = Encoding.UTF8.GetString(Decompress(base64String));

Resulting in (.Net Fidddle):

[
   "room:shard1/W1N1",
   {
      "objects":{
         "5982fe1eb097071b4adc0743":{
            "energy":2440,
            "invaderHarvested":2664
         },
         "5dad1a7cef434b7d68fd5160":{
            "store":{
               "energy":1536
            }
         },
         "5dd1cf2c884de042fb6be550":{
            "hits":4100,
            "nextDecayTime":25374980
         }
      },
      "gameTime":25373981,
      "info":{
         "mode":"world"
      },
      "visual":"{\"t\":\"r\",\"x\":37,\"y\":0,\"w\":12,\"h\":6,\"s\":{\"fill\":\"black\",\"stroke\":\"white\",\"opacity\":0.25}}\n{\"t\":\"t\",\"text\":\"CPU Usage\",\"x\":43,\"y\":1,\"s\":{\"font\":0.8}}\n{\"t\":\"l\",\"x1\":37.05,\"y1\":1.5,\"x2\":48.95,\"y2\":1.5}\n{\"t\":\"t\",\"text\":\"Current Usage\",\"x\":37.5,\"y\":2.5,\"s\":{\"align\":\"left\"}}\n{\"t\":\"t\",\"text\":\"2.43 / 4\",\"x\":46.5,\"y\":2.5}\n{\"t\":\"l\",\"x1\":37.05,\"y1\":3,\"x2\":48.95,\"y2\":3}\n{\"t\":\"t\",\"text\":\"Average Usage\",\"x\":37.5,\"y\":4,\"s\":{\"align\":\"left\"}}\n{\"t\":\"t\",\"text\":\"2.82\",\"x\":46.5,\"y\":4}\n{\"t\":\"l\",\"x1\":37.05,\"y1\":4.5,\"x2\":48.95,\"y2\":4.5}\n{\"t\":\"t\",\"text\":\"Bucket Storage\",\"x\":37.5,\"y\":5.5,\"s\":{\"align\":\"left\"}}\n{\"t\":\"t\",\"text\":9998,\"x\":46.5,\"y\":5.5}\n{\"t\":\"l\",\"x1\":44,\"y1\":1.55,\"x2\":44,\"y2\":5.95}\n",
      "users":{
         "595d14d08d05b87f33a7e33a":{
            "_id":"595d14d08d05b87f33a7e33a",
            "username":"wtfrank",
            "badge":{
               "type":22,
               "color1":"#adbaeb",
               "color2":"#265fd9",
               "color3":"#1a1a1a",
               "param":-63,
               "flip":true
            }
         }
      }
   }
]
Jimi
  • 29,621
  • 8
  • 43
  • 61
  • If I use a GzipStream like in your example I still get an error with **System.IO.InvalidDataException:** "The magic number in GZip header is not correct. Make sure you are passing in a GZip stream." – user13122655 Mar 25 '20 at 14:59
  • turns out it only works with .NET Framework 4.7.2 and I was using 4.6.1, I changed it and now it works, thank you – user13122655 Mar 25 '20 at 15:21
  • You're right. you need at least `4.7.2`. `4.7.1` also fails here. I'll add it to the notes. – Jimi Mar 25 '20 at 15:26