In addition, SharpZipLib is developed by "SharpDevelop" - I've tried the
"SharpDevelop" "IDE" before and found it appalling, so I don't actually
trust
anything written by them.
It set up a load of bogus users with random strings as their names, and
made
them owners of its directory structure, I had to fiddle around removing
them
all after I'd uninstalled it. Cygwin did exactly the same thing (only to
its
own directory structure though). (GNU people trying their hand at
Microsoft
stuff? What's that all about anyway?)
And whoever's written "SharpZipLib" - however well they've tried to
convert
it, it can only be a maximum of two years old - as that's how old C# is.
The
unmanaged version looks like it's been around a lot longer than that,
and
seems to have gone through numerous versions and bug fixes.
:
You can avoid using unsafe code:
[DllImport("zlib1.dll", CallingConvention=CallingConvention.Cdecl)]
static extern short compress(IntPtr dest, ref uint destLen, IntPtr
source,
uint sourceLen);
short compress(byte[] dest, ref uint destLen, byte[] source, uint
sourceLen)
{
// do some checking on null references...
GCHandle hDest = GCHandle.Alloc(dest, GCHandleType.pinned);
GCHandle hSource = GCHandle.Alloc(source, GCHandleType.pinned);
try
{
return compress(hDest.AddrOfPinnedObject(), ref destLen,
hSource.AddrOfPinnedObject(), sourceLen);
}
finally
{
hSource.Free();
hDest.Free();
}
}
But I would recommend using the SharpZipLib anyway.
HTH,
Stefan
"Bonj" <benjtaylor at hotpop d0t com> wrote in message
Thanks, that seems to work.
"Bonj" <benjtaylor at hotpop d0t com> wrote in message
I tried it with
[DllImport("zlib1.dll", CallingConvention=CallingConvention.Cdecl)]
static extern Int16 compress(ref byte[] dest, ref uint destlen,
byte[]
source, int sourcelen);
and it still does the same thing, bombs on the call to that method.
I also tried it with the return value as int (Int32) and that did
the
same thing aswell.
"Bonj" <benjtaylor at hotpop d0t com> wrote in message
I downloaded the gzlib library from zlib in order to do compression.
(
http://www.gzip.org/zlib)
The prototype of the compression function seems to be
int compress (Bytef *dest, uLongf *destLen, const Bytef *source,
uLong
sourceLen);
It is meant to be called by C, but I would rather use it from C#.
So I wrote the following C# program to test it, but it failed to
work.
The call to compress doesn't return or throw an exception, it
simply
bombs the program. I'm probably calling it wrong, but have no idea
why.
This is the program:
using System;
using System.IO;
using System.Security;
using System.Runtime.InteropServices;
class class1
{
// int compress (Bytef *dest, uLongf *destLen, const Bytef *source,
uLong sourceLen);
[SuppressUnmanagedCodeSecurityAttribute()]
[DllImport("zlib1.dll")]
static extern Int16 compress(ref byte[] dest, ref uint destlen,
byte[]
source, int sourcelen);
static void Main(string[] args)
{
if(args.Length != 2)
{
Console.WriteLine("Usage: testgz <inputfile> <outputfile>");
return;
}
try
{
int filelen = (int)((new FileInfo(args[0])).Length);
uint outputlen = (uint)Math.Ceiling(1.001 * filelen) + 12;
using(FileStream fsr = new FileStream(args[0], FileMode.Open,
FileAccess.Read, FileShare.Read))
{
byte[] inputbytes = new byte[filelen],
outputbytes = new byte[outputlen];
fsr.Read(inputbytes, 0, filelen);
if(compress(ref outputbytes, ref outputlen, inputbytes, filelen)
==
0)
using(FileStream fsw = new FileStream(args[1], FileMode.Create,
FileAccess.Write, FileShare.Read))
{
fsw.Write(outputbytes, 0, (int)outputlen);
fsw.Close();
}
fsr.Close();
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}