DotNetZip Library

Hey everyone. It’s been a while since I posted and I thought I would post about this awesome library that I just found recently for zipping and unzipping files and streams in .NET. It is available for both C# and VB, which is always nice to see the flexibility for developers. It seems very efficient and has a lot of documentation and starter tutorials in the download package and out on the web. Here is an example of using DotNetZip to zip a file.

  using (var zip = new ZipFile())
            {
                var entry = zip.AddFile(@"C:\FileToZip.txt");
                entry.FileName = NameToCallZip;
                zip.Save(LocationToSave);
            }

That’s basically it. now I tend to wrap SDK libraries in my own libraries to abstract things out, but this is the simplest code you can do.

So now, lets suppose your getting a Stream of some sort and you need to zip that up.

// Normally would be some stream to zip
MemoryStream data  = new MemoryStream();    
using (var zip = new ZipOutputStream(data))
            {
                zip.PutNextEntry(NameOfZipFile);
                var buffer = new byte[data.Length];
                data.Read(buffer, 0, buffer.Length);
                zip.Write(buffer, 0, buffer.Length);
                
            }

Pretty simple right? You take in the stream, create a ZipOutputStream and use that object to create the zip file. The memory stream must be read into a byte array for the zip tool to create the zip stream. ZipOutputStream inherits from Stream (the C# .NET object). This makes it very easy to pass around and utilize the same features any other stream does.

 

So this one is short and sweet, but I thought a quick tutorial on using the DotNetZip libraries would be nice to shoot out to my subscribers.

 

Happy Coding Smile

Advertisements

About Gregg Coleman

I am Senior-level Software Engineer working primarily these days with .NET. I have a good working knowledge of ASP.NET MVC, Web Forms, WCF web services and Windows Services. I spend much of my time in the Web Services (SOAP and REST) world in my current job designing and implementing various SOA architectures. I have been in the software engineering industry for about 6 years now and will not now nor ever consider myself an "expert" in programming because there is always so much to learn. My favorite thing about designing software is there are always new emerging technologies and something to learn every day! My current job has me spending much of my job on the bleeding edge of technologies and changing gears all the time, so I'm never bored and always challenged. On my spare time I enjoy weight training, reading and venturing to new places near by. Of course programing and learning new technologies are another hobby of mine.
This entry was posted in .NET, .NET 4.5, ASP.NET, C#, Classes, DataAnnotation, SQL Server and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s