Hi there. Yes, there are zlib librarys written in pure .net but I prefer to use the unmanaged zlib1.dll library as it seems to lack some of the memory leaks evident in .net versions.
Anyway, a long time ago I made a wrapper to use it in my c# apps:
Code:
public class Zip
{
[DllImport("zlib1.dll")]
static extern int compress(byte[] destBuffer, ref uint destLen, byte[] sourceBuffer, uint sourceLen);
[DllImport("zlib1.dll")]
static extern int uncompress(byte[] destBuffer, ref uint destLen, byte[] sourceBuffer, uint sourceLen);
public static byte[] Compress(byte[] data)
{
uint _dLen = (uint)((data.Length * 1.1) + 12);
byte[] _d = new byte[_dLen];
compress(_d, ref _dLen, data, (uint)data.Length);
byte[] result = new byte[_dLen];
Array.Copy(_d, 0, result, 0, result.Length);
return result;
}
public static byte[] Decompress(byte[] data)
{
uint _dLen = 8192;
byte[] _d = new byte[_dLen];
if (uncompress(_d, ref _dLen, data, (uint)data.Length) != 0)
return null;
byte[] result = new byte[_dLen];
Array.Copy(_d, 0, result, 0, result.Length);
return result;
}
This works perfectly fine in .net versions 2, 3 and 3.5. Unfortunately it is throwing an exception in VS2010 if I have selected 4.0 (or 4.0 CP):
Code:
A call to PInvoke function 'WindowsFormsApplication1!WindowsFormsApplication1.Zip::compress' has unbalanced the stack.
This is likely because the managed PInvoke signature does not match the unmanaged target signature.
Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
Any thoughts?