Geeks With Blogs
Stuart Brierley Integration Management - BizTalk and More

In a project that I completed a few years ago I had need to programatically zip an xml file for use as an email attachment. 

This specific implementation was a part of a BizTalk Adapter written in VB.Net, but the code library used to carry out the zip process was a freely available C# library that can be used in any .Net code for zipping purposes.  The library used was called SharpZipLib.

The .Net SharpZipLib library (formally NZipLib) supports Zip as well as GZip format and comes with C# source code included.

The library is distributed under the GNU General Public License (GPL), but thanks to an exemption from the developers it may be used in non-GPL applications:

"Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination.

As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.

Bottom line In plain English this means you can use this library in commercial closed-source applications."

To use this code library I first referenced the dll in my VB.Net project, and then included the following imports statements:

Imports ICSharpCode.SharpZipLib.Core
Imports ICSharpCode.SharpZipLib.Zip

I then implemented the zip process using the following code to load a file and save the zipped file to a directory location:

            Dim strmZipOutputStream As ZipOutputStream

            strmZipOutputStream = New ZipOutputStream(File.Create(TemporaryAttachmentFileFullPath))

            REM Compression Level: 0-9
            REM 0: no(Compression)
            REM 9: maximum compression

            Dim strmFile As FileStream = File.OpenRead(SourceFileFullPath)
            Dim abyBuffer(strmFile.Length - 1) As Byte

            strmFile.Read(abyBuffer, 0, abyBuffer.Length)
            Dim objZipEntry As ZipEntry = New ZipEntry(ContainedFileName)

            objZipEntry.DateTime = System.DateTime.Now
            objZipEntry.Size = strmFile.Length
            strmZipOutputStream.Write(abyBuffer, 0, abyBuffer.Length)


Hopefully this will be of some use to someone out there, if not it's at least a good reminder to myself!


Posted on Tuesday, November 10, 2009 4:39 PM ASP.Net / VB.Net / C# / IIS | Back to top

Comments on this post: .Net Zip Library

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Stuart Brierley | Powered by: