0

We are building a Unity-based game and faced a problem with DeflateStream on Windows. (We are trying to compress packages being sent through websocket)

It seems that Unity doesn't support zip on some platforms:

System.DllNotFoundException: MonoPosixHelper
  at (wrapper managed-to-native) System.IO.Compression.DeflateStream:CreateZStream (System.IO.Compression.CompressionMode,bool,System.IO.Compression.DeflateStream/UnmanagedReadOrWrite,intptr)
  at System.IO.Compression.DeflateStream..ctor (System.IO.Stream compressedStream, CompressionMode mode, Boolean leaveOpen, Boolean gzip) [0x00000] in <filename unknown>:0 
  at System.IO.Compression.DeflateStream..ctor (System.IO.Stream compressedStream, CompressionMode mode, Boolean leaveOpen) [0x00000] in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) System.IO.Compression.DeflateStream:.ctor (System.IO.Stream,System.IO.Compression.CompressionMode,bool)
  at WebSocketSharp.Ext.compress (System.IO.Stream stream) [0x00000] in <filename unknown>:0 
  at WebSocketSharp.Ext.Compress (System.IO.Stream stream, CompressionMethod method) [0x00000] in <filename unknown>:0 
  at WebSocketSharp.WebSocket.send (Opcode opcode, System.IO.Stream stream) [0x00000] in <filename unknown>:0 
UnityEngine.Debug:Log(Object)
ServerConnection:<connectToServer>m__7(Object, ErrorEventArgs) (at Assets/Scripts/Network/ServerConnection.cs:83)
WebSocketSharp.Ext:Emit(EventHandler`1, Object, ErrorEventArgs)
WebSocketSharp.WebSocket:error(String, Exception)
WebSocketSharp.WebSocket:send(Opcode, Stream)
WebSocketSharp.WebSocket:Send(String)
ServerConnection:sendRegInfo() (at Assets/Scripts/Network/ServerConnection.cs:108)
ServerConnection:onOpen() (at Assets/Scripts/Network/ServerConnection.cs:58)
ServerConnection:<connectToServer>m__4(Object, EventArgs) (at Assets/Scripts/Network/ServerConnection.cs:77)
WebSocketSharp.Ext:Emit(EventHandler, Object, EventArgs)
WebSocketSharp.WebSocket:open()

We also found some information about this error:

Mono implements System.IO.Compression.GZipStream not with a managed implementation, but it relies on a system installed zlib instead

The question is - are there simple variants? Maybe we can provide zlib in our project or something else simple? We know about SharpZipLib but it has a bit strange API.

  • Unity may have additional cross-platform considerations, but some of the answers on [this question](http://stackoverflow.com/questions/6341517/zip-lib-zlib-bzip2-and-mono-support) might apply. – rutter Apr 14 '15 at 15:57

1 Answers1

0

I can't say about recent version of Unity, but I've got experience with compressing on previous (3.x, 4.x) and I think the situation did not change so much.

Unity is built on the top old version mono (v2.6) with some hacks from Unity team and have never upgraded to recent versions of mono during whole Unity lifetime. This means that Unity inherits all old mono bugs, which were fixed in the latest versions of mono. One of this bug was the bug with deflate stream Mono & DeflateStream. It has being fixed pretty fast in mono, but has not being fixed in Unity.

To workaround this bug for my Unity web application, I have to use managed implementation of Deflate stream. I decided to use Ionic.Zlib, it has the same interface as System.IO.Compression.

What is bad that it's performance roughly 3 times worse than native mono implementation, but adding ifdefs in the source resolves the performance issue at least for server code.

#if UNITY
using Ionic.Zlib;
#else
using System.IO.Compression;
#endif
Community
  • 1
  • 1
Sergey Zhukov
  • 1,342
  • 1
  • 13
  • 24