How to read base64 zlib data on tmx files?

Hi, i’m writing a C# script for an Unity plugin I’m developing. I need to decode and uncompress the string inside data and get a readable output.

Unfortunately mono doesn’t implement System.IO.Compression so that I’m using Ionic.Zlib.

This is the sintax I’m using is pretty simple:

public static string DecodeBase64Gzip(string input)	
{
byte[ ] ZlibBuffer = Convert.FromBase64String(input);
MemoryStream comp = new MemoryStream(ZlibBuffer);
Stream zlib = new Ionic.Zlib.ZlibStream (comp, Ionic.Zlib.CompressionMode.Decompress);
return new System.IO.StreamReader (zlib, System.Text.Encoding.UTF8).ReadToEnd();
}

I’m doing some mistake because I’m getting an empty string.

Have you some advice? Thanks to anyone could help!

While I cannot help you with your direct issue (I never used that library myself), do you plan to do any specific manipulations? Because there are some libraries for loading tmx maps using c# / Unity:

https://github.com/marshallward/TiledSharp looks interesting here… However: I never used any of those libraries myself! So I have no idea how good they are… (and wether they meet your needs)

Regards,
Ablu

Ok, fixed up!

Fore anyone interested here the solution for the zlib compression, it’s quite the same for gzip:

assuming that the zipped string is correctly stored in layer.data.value:

var decodedStream = TmxUnzip.DecodeBase64Zlib(layer.data.value);
using (var bn = new BinaryReader(decodedStream)){
	for (int j = 0; j < tmxData.height; j++){
	        for (int i = 0; i < tmxData.width; i++){
                 var gid = bn.ReadUInt32();
		     Console.WriteLine(gid);
	        }
	}
}

// where the function for unpack the zipped string is:

public static Stream DecodeBase64Zlib(string input)
{
	byte[ ] ZlibBuffer = Convert.FromBase64String(input);
	MemoryStream comp = new MemoryStream(ZlibBuffer);
	return new Ionic.Zlib.ZlibStream(comp, Ionic.Zlib.CompressionMode.Decompress);
}

Thanks for posting your solution after fixing it yourself :smile:

Regards,
Ablu