In Memory compression using SharpZipLib

8. June 2010 17:13 by viperguynaz in .NET, WCF  //  Tags: , , ,   //   Comments (2)

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!

 


Comments (2) -

Shaun
10/22/2010 4:20:07 AM #

Hey there, this is great and thanks but did you figure out decompression using MemoryStream? I can't seem to get it to work and information about it seems to be scarce. Cheers.

Chris
8/31/2011 2:28:04 PM #

wow, a sharpziblip tutorial witha PUNCH! thank you

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About the author

After 20 years flying F-16s in the USAF, I've had a ton of fun and I'm now doing something I enjoy almost as much, developing code.  I currently work as an ASP.NET/MVC developer for SpinSix Strategic Marketing Design in Scottsdale AZ.  I love developing ASP.NET MVC and eCommerce sites.

Month List

Page List