0
\$\begingroup\$

I am trying to decode a map file for Unity which I believe was compressed using Base64 encoding.

The actual data looks like this:

H4sIAHah5VgA/z2PvQ7CMAyEXyXyjASsXStGeAHEkBRTivJTOQlKW/XdSYKc7dOd7+Tb4CrnmzQIHQzRB2eExi9qOAiQMbwdZeOFdiyCUx/o7mKD6ZnVc5EIfca+JnPTRaNBG459VFjslM1TgYVhZfCJO/zSaG29LUktSi1LfDYyKAbJEDCFSGVXbR5omkP51UZd19n/6PrpLh77D92WFGkKAQAAAA=

And I am expecting an output that looks like:

custom,cuboid,default,1,1,1,0,1,1,1,1.0,1.0,0,0,0,0,0,0,0;

While Base64 decoding should be an easy feat, this particular case is giving me trouble. I've tried decoding it into several different formats, but all my attempts resulted in garbled messes. I am fairly certain that the file is in a Base64 format, but it shouldn't be this difficult to decode.

Does anyone have an idea of how the file was encoded and how I might go about decoding it?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Converting your base64 string to hex shows it starts with 1F 8B which is the magic number for GZIP compressed data. This code works as expected:

byte[] data = Convert.FromBase64String("H4sIAHah5VgA/z2PvQ7CMAyEXyXyjASsXStGeAHEkBRTivJTOQlKW/XdSYKc7dOd7+Tb4CrnmzQIHQzRB2eExi9qOAiQMbwdZeOFdiyCUx/o7mKD6ZnVc5EIfca+JnPTRaNBG459VFjslM1TgYVhZfCJO/zSaG29LUktSi1LfDYyKAbJEDCFSGVXbR5omkP51UZd19n/6PrpLh77D92WFGkKAQAAAA==");
using (var compressedStream = new MemoryStream(data))
using (var gZipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
using (var resultStream = new MemoryStream())
{
    gZipStream.CopyTo(resultStream);
    string map = Encoding.UTF8.GetString(resultStream.ToArray());
}
\$\endgroup\$
1
  • \$\begingroup\$ Thanks a lot! That worked perfectly for my problem. I didn't realize it was in the GZIP format. Ill definitely remember that trick you used to classify the encoding type. Ill also look into other encoding types for future reference. Thanks again. \$\endgroup\$ Commented Apr 6, 2017 at 17:34

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.