B
Bonj
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());
}
}
}
(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());
}
}
}