Cant use GZipStream from C# behaviours

When trying to use GZipStream from a c# behaviour i get an exeption (This occurs only on my mac box)

System.DllNotFoundException: MonoPosixHelper
  at (wrapper managed-to-native) System.IO.Compression.DeflateStream:create_z_stream (System.IO.Compression.CompressionMode,bool)
  at System.IO.Compression.DeflateStream..ctor (System.IO.Stream compressedStream, CompressionMode mode, Boolean leaveOpen, Boolean gzip) [0x00000] 
  at (wrapper remoting-invoke-with-check) System.IO.Compression.DeflateStream:.ctor (System.IO.Stream,System.IO.Compression.CompressionMode,bool,bool)
  at System.IO.Compression.GZipStream..ctor (System.IO.Stream compressedStream, CompressionMode mode, Boolean leaveOpen) [0x00000] 
  at System.IO.Compression.GZipStream..ctor (System.IO.Stream compressedStream, CompressionMode mode) [0x00000] 
  at (wrapper remoting-invoke-with-check) System.IO.Compression.GZipStream:.ctor (System.IO.Stream,System.IO.Compression.CompressionMode)
  at GZIPDecompressor.Decompress (System.Byte[] data) [0x00042] in /Users/peterbjornx/runescape client 3/runescape client/assets/runescape scripts/cache/GZIPDecompressor.cs:20

Unity is limited to the classes that come with the Mono runtime - System.IO.Compression isn’t among these.

With a pro license is it possible to drop in a mono dll that will deal with gzip compression?

no need for pro, .net dlls always work

unsure if this is possible for framework dlls though (as they will cause collisions)
also the dll must not use namespaces cut from the webplayer at all

I’ve got this working for the mac standalone and it also appears to work for the webplayer.

I’ve used the SharpZipLib .NET source and compiled it with mono.

Then used this code from the Code Project to compress and uncompress a string.

http://www.codeproject.com/KB/dotnet/mscompression.aspx

using System;
using System.IO;
using System.Text;
using ICSharpCode.SharpZipLib.BZip2;

[System.Serializable]
public struct Zipper
{
	public static string ZipString(string sBuffer)
	{
		MemoryStream m_msBZip2 = null;
		BZip2OutputStream m_osBZip2 = null;
		string result;
		try
		{
			m_msBZip2 = new MemoryStream();
			Int32 size = sBuffer.Length;
			// Prepend the compressed data with the length of the uncompressed data (firs 4 bytes)
			//
			using (BinaryWriter writer = new BinaryWriter(m_msBZip2, System.Text.Encoding.ASCII))
			{
				writer.Write( size );
				
				m_osBZip2 = new BZip2OutputStream(m_msBZip2);
				m_osBZip2.Write(Encoding.ASCII.GetBytes(sBuffer), 0, sBuffer.Length);
				
				m_osBZip2.Close();
				result = Convert.ToBase64String(m_msBZip2.ToArray());
				m_msBZip2.Close();
				
				writer.Close();
			}
		}
		finally
		{
			if (m_osBZip2 != null)
			{
				m_osBZip2.Dispose();
			}
			if (m_msBZip2 != null)
			{
				m_msBZip2.Dispose();
			}
		}
		return result;
	}

	public static string UnzipString(string compbytes)
	{
		string result;
		MemoryStream m_msBZip2 = null;
		BZip2InputStream m_isBZip2 = null;
		try
		{
			m_msBZip2 = new MemoryStream(Convert.FromBase64String(compbytes));
			// read final uncompressed string size stored in first 4 bytes
			//
			using (BinaryReader reader = new BinaryReader(m_msBZip2, System.Text.Encoding.ASCII))
			{
				Int32 size = reader.ReadInt32();
				
				m_isBZip2 = new BZip2InputStream(m_msBZip2);
				byte[] bytesUncompressed = new byte[size];
				m_isBZip2.Read(bytesUncompressed, 0, bytesUncompressed.Length);
				m_isBZip2.Close();
				m_msBZip2.Close();
				
				result = Encoding.ASCII.GetString(bytesUncompressed);
				
				reader.Close();
			}
		}
		finally
		{
			if (m_isBZip2 != null)
			{
				m_isBZip2.Dispose();
			}
			if (m_msBZip2 != null)
			{
				m_msBZip2.Dispose();
			}
		}
		return result;
	} 
}

253802–9145–$icsharpcodesharpziplibdll_923.zip (77.4 KB)

Hello monark

Is it possible to call your functions from Javascript?

Thanks

I figured it out.

Thank you for sharing your code!

It works great. :smile:

For others who pass this way, yes it is…

Have you ever tried to make it work on a Windows build?

yup, seems to be fine in my tests so far, have you found otherwise?

I have not tried that yet. But since the DLL is necessary within the Plugins folder I thought a similar file would be necessary for the Windows build.

I think it just bundles it all up for you automatically as it’s a mono dll, I didn’t do anything special to include it in the windows standalone version I tried.

Hi , could someone explain how to use this with JavaScript ?

Just download the dll I included with my original post and include it in your assets folder. I put it in a sub folder called “Plugins” but I don’t think it matters where you put it.
Then in javascript you should be free to use the ZipString and UnzipString functions any where you like just as you would any builtin function.

e.g

var myString : String = "Hello world";

var myZippedString : String = Zipper.ZipString(myString);

var myUnZippedString : String = Zipper.UnzipString(myZippedString);

Hum , it should work just like that ?

Because I get this error :

"Unknown identifier : ‘Zippper’ "

Sounds like it’s not picking up the dll properly.

Do you get any compile error messages that relate to Zipper other than the one you posted?

Unless it’s cos I compiled it on a mac and you are on windows?

@MatOfLink - Have you included Monark’s code from further up the thread? Zipper is defined in there.

On a side note, this is good stuff and I will try and use it within a project.

Best Regards,
Matt.

Oh yeah, doh! you need that bit of code too. I forgot about that the dll is the compressor but it needs that wrapper code too. Just make a C Sharp script copy that code into it and put it somewhere like “Assets/Pro Standard Assets”

Oh damn, I’ve been refreshing page 1 since I posted so I hadn’t seen your answer yet xD

So, thank you, it works perfectly :wink:

[Edit]

Ok it works with String.
Now here is my new problem :

I get a file (which is zipped) using WWW. How should I use Zipper to decompress the data within it ?
I don’t want to unzip the file physically, just get the data and decompress it in order to read it.

if you use above Zipper, just through UnzipString with the www.data