Cant use GZipStream from C# behaviours

Discussion in 'Unity Support' started by Peterbjornx, Nov 8, 2009.

  1. Peterbjornx

    Peterbjornx

    New Member

    Joined:
    Nov 8, 2009
    Messages:
    5
    When trying to use GZipStream from a c# behaviour i get an exeption (This occurs only on my mac box)


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

    andeeeee

    Unity Technologies

    Joined:
    Jul 19, 2005
    Messages:
    8,729
    Unity is limited to the classes that come with the Mono runtime - System.IO.Compression isn't among these.
  3. monark

    monark

    Member

    Joined:
    May 2, 2008
    Messages:
    1,106
    With a pro license is it possible to drop in a mono dll that will deal with gzip compression?
  4. Dreamora

    Dreamora

    Member

    Joined:
    Apr 5, 2008
    Messages:
    26,586
    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
  5. monark

    monark

    Member

    Joined:
    May 2, 2008
    Messages:
    1,106
    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.

    http://www.icsharpcode.net/OpenSource/SharpZipLib/

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

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

    Code (csharp):
    1.  
    2.  
    3. using System;
    4. using System.IO;
    5. using System.Text;
    6. using ICSharpCode.SharpZipLib.BZip2;
    7.  
    8. [System.Serializable]
    9. public struct Zipper
    10. {
    11.     public static string ZipString(string sBuffer)
    12.     {
    13.         MemoryStream m_msBZip2 = null;
    14.         BZip2OutputStream m_osBZip2 = null;
    15.         string result;
    16.         try
    17.         {
    18.             m_msBZip2 = new MemoryStream();
    19.             Int32 size = sBuffer.Length;
    20.             // Prepend the compressed data with the length of the uncompressed data (firs 4 bytes)
    21.             //
    22.             using (BinaryWriter writer = new BinaryWriter(m_msBZip2, System.Text.Encoding.ASCII))
    23.             {
    24.                 writer.Write( size );
    25.                
    26.                 m_osBZip2 = new BZip2OutputStream(m_msBZip2);
    27.                 m_osBZip2.Write(Encoding.ASCII.GetBytes(sBuffer), 0, sBuffer.Length);
    28.                
    29.                 m_osBZip2.Close();
    30.                 result = Convert.ToBase64String(m_msBZip2.ToArray());
    31.                 m_msBZip2.Close();
    32.                
    33.                 writer.Close();
    34.             }
    35.         }
    36.         finally
    37.         {
    38.             if (m_osBZip2 != null)
    39.             {
    40.                 m_osBZip2.Dispose();
    41.             }
    42.             if (m_msBZip2 != null)
    43.             {
    44.                 m_msBZip2.Dispose();
    45.             }
    46.         }
    47.         return result;
    48.     }
    49.  
    50.     public static string UnzipString(string compbytes)
    51.     {
    52.         string result;
    53.         MemoryStream m_msBZip2 = null;
    54.         BZip2InputStream m_isBZip2 = null;
    55.         try
    56.         {
    57.             m_msBZip2 = new MemoryStream(Convert.FromBase64String(compbytes));
    58.             // read final uncompressed string size stored in first 4 bytes
    59.             //
    60.             using (BinaryReader reader = new BinaryReader(m_msBZip2, System.Text.Encoding.ASCII))
    61.             {
    62.                 Int32 size = reader.ReadInt32();
    63.                
    64.                 m_isBZip2 = new BZip2InputStream(m_msBZip2);
    65.                 byte[] bytesUncompressed = new byte[size];
    66.                 m_isBZip2.Read(bytesUncompressed, 0, bytesUncompressed.Length);
    67.                 m_isBZip2.Close();
    68.                 m_msBZip2.Close();
    69.                
    70.                 result = Encoding.ASCII.GetString(bytesUncompressed);
    71.                
    72.                 reader.Close();
    73.             }
    74.         }
    75.         finally
    76.         {
    77.             if (m_isBZip2 != null)
    78.             {
    79.                 m_isBZip2.Dispose();
    80.             }
    81.             if (m_msBZip2 != null)
    82.             {
    83.                 m_msBZip2.Dispose();
    84.             }
    85.         }
    86.         return result;
    87.     }
    88. }
    89.  

    Attached Files:

  6. bakno

    bakno

    Member

    Joined:
    Mar 18, 2007
    Messages:
    460
    Hello monark

    Is it possible to call your functions from Javascript?

    Thanks
  7. bakno

    bakno

    Member

    Joined:
    Mar 18, 2007
    Messages:
    460
    I figured it out.

    Thank you for sharing your code!

    It works great. :D
  8. monark

    monark

    Member

    Joined:
    May 2, 2008
    Messages:
    1,106
    For others who pass this way, yes it is....
  9. bakno

    bakno

    Member

    Joined:
    Mar 18, 2007
    Messages:
    460
    Have you ever tried to make it work on a Windows build?
  10. monark

    monark

    Member

    Joined:
    May 2, 2008
    Messages:
    1,106
    yup, seems to be fine in my tests so far, have you found otherwise?
  11. bakno

    bakno

    Member

    Joined:
    Mar 18, 2007
    Messages:
    460
    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.
  12. monark

    monark

    Member

    Joined:
    May 2, 2008
    Messages:
    1,106
    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.
  13. MatOfLink

    MatOfLink

    New Member

    Joined:
    Jun 22, 2009
    Messages:
    31
    Hi , could someone explain how to use this with JavaScript ?
  14. monark

    monark

    Member

    Joined:
    May 2, 2008
    Messages:
    1,106
    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

    Code (csharp):
    1.  
    2. var myString : String = "Hello world";
    3.  
    4. var myZippedString : String = Zipper.ZipString(myString);
    5.  
    6. var myUnZippedString : String = Zipper.UnzipString(myZippedString);
    7.  
    8.  
  15. MatOfLink

    MatOfLink

    New Member

    Joined:
    Jun 22, 2009
    Messages:
    31
    Hum , it should work just like that ?

    Because I get this error :

    "Unknown identifier : 'Zippper' "
  16. monark

    monark

    Member

    Joined:
    May 2, 2008
    Messages:
    1,106
    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?
  17. fallingbrickwork

    fallingbrickwork

    Member

    Joined:
    Mar 16, 2009
    Messages:
    1,066
    @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.
  18. monark

    monark

    Member

    Joined:
    May 2, 2008
    Messages:
    1,106
    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"
  19. MatOfLink

    MatOfLink

    New Member

    Joined:
    Jun 22, 2009
    Messages:
    31
    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 ;)

    [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.
  20. Dreamora

    Dreamora

    Member

    Joined:
    Apr 5, 2008
    Messages:
    26,586
    if you use above Zipper, just through UnzipString with the www.data
  21. MatOfLink

    MatOfLink

    New Member

    Joined:
    Jun 22, 2009
    Messages:
    31
    Yes , that's what I've tried. Yet I get this error :
    Code (csharp):
    1. EndOfStreamException: Failed to read past end of stream.
    2. System.IO.BinaryReader.FillBuffer (Int32 bytes)
    3. System.IO.BinaryReader.ReadInt32 ()
    I'm quiet sure the file is not corrupted because when I manually unZip the file and then read it with Unity using www.data, the data are correct.
  22. Dreamora

    Dreamora

    Member

    Joined:
    Apr 5, 2008
    Messages:
    26,586
    That error on the other end looks hell familiar ...
    Just that I get it in my case when trying to deserialize a formaly binary serialized object.

    I've not solved the mystery behind this yet but given that the XML serializer we have is working I'm forced to assume that Mono 1.2.5 binaryreader is at fault here
  23. MatOfLink

    MatOfLink

    New Member

    Joined:
    Jun 22, 2009
    Messages:
    31
    So .. does anybody have a solution for me ? :roll:
  24. monark

    monark

    Member

    Joined:
    May 2, 2008
    Messages:
    1,106
    I've only used it on strings and not seen any issues, am I right that you are trying to use this on binary data?
  25. MatOfLink

    MatOfLink

    New Member

    Joined:
    Jun 22, 2009
    Messages:
    31
  26. RSH1

    RSH1

    New Member

    Joined:
    Jul 9, 2012
    Messages:
    100
    Did anyone every get to the bottom of this, I'm also getting the EndOfStreamException.

    EDIT: I think this was because I hadn't made my receive buffer large enough, seems to be working now.
    Last edited: Oct 29, 2013