If you are looking for an open source compressions library for .NET's C#, book on over to http://www.icsharpcode.net/opensource/sharpziplib/ and check out their open source library. It compiles easily in .NET Framework 2.0-4.0. From their description:
#ziplib (SharpZipLib, formerly NZipLib) is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform. It is implemented as an assembly (installable in the GAC), and thus can easily be incorporated into other projects (in any .NET language). The creator of #ziplib put it this way: "I've ported the zip library over to C# because I needed gzip/zip compression and I didn't want to use libzip.dll or something like this. I want all in pure C#."
I've used the library in several projects. Most recently, I had a requirement to use the library to do compression in-memory (no file I/O) for the purpose of creating an MTOM attachment to a WCF message.
// zip XElement xdoc and add to requests MTOM value
using (MemoryStream ms = new System.IO.MemoryStream())
{
xdoc.Save(ms);
ms.Position = 0;
// create the ZipEntry archive from the xml doc store in memory stream ms
using (MemoryStream outputMS = new System.IO.MemoryStream())
{
using (ZipOutputStream zipOutput = new ZipOutputStream(outputMS))
{
ZipEntry ze = new ZipEntry("example.xml");
zipOutput.PutNextEntry(ze);
zipOutput.Write(ms.ToArray(), 0, Convert.ToInt32(ms.Length));
zipOutput.Finish();
zipOutput.Close();
// add the zip archive to the request
SubmissionReceiptListAttachmentMTOM = new base64Binary();
SubmissionReceiptListAttachmentMTOM.Value = outputMS.ToArray();
}
outputMS.Close();
}
ms.Close();
}
// for debugging
writeByteArrayToFile(SubmissionReceiptListAttachmentMTOM.Value, "test.zip");
The code fragment above creates a MemoryStream and then saves an already existing LINQ XElement to the MemoryStream. The SharpZipLib class ZipOutputStream is used to create the compressed archive and then uses the MemoryStream method ToArray() to write the zip archive to the MTOM attachment of the message.
Cheers...Don!