[SOLVED]Binary Data Compression with DeflateStream

I am attempting to export compressed terrain heightmaps from the Unity editor, and later download and uncompress them from a Unity webplayer. I have the code to export and import terrain heightmaps working perfectly, but can’t figure out the compression side of things.

Here’s my code:

import System.IO.Compression;

...

var trnDat : TerrainData = trn.terrainData;
//Save Heightmap
var hmap = trnDat.GetHeights(0, 0, trnDat.heightmapWidth, trnDat.heightmapHeight);
var ms : MemoryStream = new MemoryStream();
var br : BinaryWriter = new BinaryWriter(ms);
for (var x : int = 0; x < trnDat.heightmapWidth; x++) for (var y : int = 0; y < trnDat.heightmapHeight; y++) {
	var val : System.UInt16 = hmap[x, y] * 65535;
	br.Write(val);
}
var buffer : byte[] = ms.ToArray();
ms.Close();
br.Close();
ms = new MemoryStream();
stream = new DeflateStream(ms, CompressionMode.Compress, true);
stream.Write(buffer, 0, buffer.Length);
File.WriteAllBytes(whirld.path + tr.gameObject.name + ".trc", ms.ToArray());
stream.Close();

When run, this code results in a “DllNotFoundException: MonoPosixHelper System.IO.Compression.DeflateStream…ctor (System.IO.Stream compressedStream, CompressionMode mode, Boolean leaveOpen, Boolean gzip)” error on the “stream = new DeflateStream” line.

Here’s Microsoft’s documentation on the DeflateStream class.

Am I using the library incorrectly, or is the compression library not supported by Unity? If the latter, what options do I have?

Thanks,

-Aubrey

Case closed!

At Mike_Mac’s suggestion, I gave up on the built in .net System.IO.Compression library and incorporated dotnetzip in my project.

I am pleased to say that it works great!

How do you integrate Dotnetzip in your project?

On the original problem: System.IO (as process and registry access) are not available in the webplayer. Thats why it likely failed.