Can't compress mono?

I am trying to compress some data in mono:

public static string Save(FlightProgram program, bool compressed) { using (MemoryStream ms = new MemoryStream()) { BinaryFormatter f = new BinaryFormatter(); if (compressed) { using (DeflateStream gz = new DeflateStream(ms, CompressionMode.Compress)) { f.Serialize(gz, program); } } else { f.Serialize(ms, program); } return Convert.ToBase64String(ms.ToArray()).Replace('/', '_'); } } 

I just get the "CreateZStream" exception. There is no internal exception. What's going on here?

Stacktrace:

 Could not save flight program: CreateZStream at 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) [0x00000] in <filename unknown>:0 at KSPComputerModule.ProgramSerializer.Save (KSPComputer.FlightProgram program, Boolean compressed) [0x00000] in <filename unknown>:0 at KSPComputerModule.FPComputer.OnSave (.ConfigNode node) [0x00000] in <filename unknown>:0 (State: None) 
+7
c # mono unity3d deflatestream
source share
2 answers

Surely not the OP anymore ... but I finally managed to solve it myself. This is a bug in deploying Mono for Windows. If you delete MonoPosixHelper.dll files from the Mono / GtkSharp directories, you will find that this unexpectedly works. Running Process Monitor, you can see that instead of switching to compiled MonoPosixHelper.dll it now goes for libMonoPosixHelper.dll ... which is compiled correctly.

So, to finally solve the problem, delete all MonoPosixHelper.dll files to use libMonoPosixHelper.dll ... or, preferably, use dllmap by adding

  <dllmap dll="MonoPosixHelper" target="libMonoPosixHelper.dll" os="windows" /> 

either C:\Program Files (x86)\Mono\lib\mono\gac\System\4.0.0.0__b77a5c561934e089\System.dll.config (create if it does not already exist), or the global Mono configuration in C:\Program Files (x86)\Mono\etc\mono\config

+6
source share

I tested it myself, and I get a DllNotFoundException:

 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) [0x00000] in <filename unknown>:0 at (wrapper remoting-invoke-with-check) System.IO.Compression.DeflateStream:.ctor (System.IO.Stream,System.IO.Compression.CompressionMode) 

It looks like it's a CreateZStream function, which is an external method that needs to be defined inside MonoPosixHelper.dll.

CreateZStream is declared as

 [DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr CreateZStream(CompressionMode compress, bool gzip, DeflateStream.UnmanagedReadOrWrite feeder, IntPtr data); 

However, the file "MonoPosixHelper.dll" does not exist. There's libMonoPosixHelper.dylib, which seems to implement an entry point for CreateZStream, however, that library for mac.

Personally, I have had poor experience with most Unity compression codes / libraries, since most implementations simply port their own code library. According to the compatibility page , the class is supported, however, it only looks like the managed part is implemented . This one also seems to confirm this.

I have successfully used the SharpZipLib library in a large number of projects. This is a clean managed library with no native code dependencies. I used this library in standalone networks, Android and iOS builds without any problems.

+4
source share

All Articles