Difference in downloaded www.byte.length on iOS vs PC

I have a gzip file that I’m pulling from the web. On PC everything works great, but on iOS it comes out wrong, the file that’s decompressed is JSON and won’t parse. (I tried caching the file downloaded onto PC, then running it through the same function passing TextAsset.bytes instead of the WWW.bytes and it unzips and parses correctly on iOS.) The www.byte.length on PC is always 74616 across two different PCs, every time. On iOS the size is 74496. I can’t think of anything that could cause this change. I’ve confirmed the exact same form data is being sent on both platforms, so the return should be the exact same.

**Quite an epic battle :slight_smile: **

It’s been quite the dragon. It seems that Unity on Mac with WWW will automatically decompresses the bytes in a WWW.bytes array on download, where on Windows Unity, the bytes are not automatically decompressed.

I have now successfully made a solution which required me to check if the bytes were compressed and if the bytesDownloaded is not the same as the returned content length, then to assume they have not be decompressed and so manually decompress.

To do the decompression I downloaded the very nice gzip compression library DotNetZipLib and use the Ionic.Zlib.dll in the plugins folder. The native .net decompression librarys have been disabled in Unity for various reasons. This library can be downloaded from codeplex here.

The code I wrote to check for compression and return the actual raw bytes is :

protected byte[] GetRawBytes(WWW w) {

			string encoding = "";
			if (w.responseHeaders.TryGetValue("Content-Encoding", out encoding)) {
				string length = "";
				if (w.responseHeaders.TryGetValue("Content-Length", out length)) {
					if (w.bytesDownloaded.ToString() != length) {
						byte[] decompressedBytes = CompressionIO.Decompress_Gzip(w.bytes);
						return decompressedBytes;
					}
				}
			}

			return w.bytes;

		}

The CompressionIO class is:

using System;
using System.IO;
//using System.IO.Compression;
using Ionic.Zlib;

namespace MantleEngine.IO {


	public class CompressionIO  {
		
			

			public static byte[] Decompress_Gzip(byte[] gzip)
			{
				// Create a GZIP stream with decompression mode.
				// ... Then create a buffer and write into while reading from the GZIP stream.
				using (GZipStream stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress))
				{
					const int size = 4096;
					byte[] buffer = new byte;
					using (MemoryStream memory = new MemoryStream())
					{
						int count = 0;
						do
						{
							count = stream.Read(buffer, 0, size);
							if (count > 0)
							{
								memory.Write(buffer, 0, count);
							}
						}
						while (count > 0);
						return memory.ToArray();
					}
				}
			}

			static void testexample()
			{
				// Open a compressed file on disk.
				// ... Then decompress it with the method below.
				// ... Then write the length of each array.
				byte[] file = File.ReadAllBytes("C:\\perlgzips\\~stat.gz");
				byte[] decompressed = Decompress_Gzip(file);
				Console.WriteLine(file.Length);
				Console.WriteLine(decompressed.Length);
			}



		}


	}

I hope this helps someone and saves them similar pains.

Cheers,

Isaac