How to register a global hotkey for your application in C#

How to register a global hotkey for your application in C#

Register a hotkey for your application that can be triggered even if your form is minimized and does not have focus. More »

How to download a file in C# (progressbar and download speed)

How to download a file in C# (progressbar and download speed)

Learn how to download files in C# while displaying the percentage and the download speed. More »

UDP hole punching implementation in C#

UDP hole punching implementation in C#

Learn how to implement UDP hole punching so you can make your clients life a lot easier by not forcing them to open ports on their end. More »

 

Create and extract .zip files in C#

Sadly there aren’t many flexible or efficient ways to create .zip files in .NET prior to .NET 4.5. Thankfully some people took the initiative and created some very easy to use libraries for creating/extracting and updating .zip files. My two all time favourite are DotNetZip and SharpZipLib.

For this example I will be using the DotNetZip library.

First you will need to download the library (.dll) either from http://dotnetzip.codeplex.com/ or from http://www.fluxbytes.com/?dl_name=DotNetZipLib_v1.9.1.8.rar. The file should contain quite a few libraries, so choose the one that suits your needs the most and add it as a reference in your project.

I’ve constructed two methods for this example, one suitable for creating a .zip file with any files or folders you give it as an argument and one for extracting the contents of a .zip file to any location you want.

Create .zip file method:

Usage:

Extract .zip file method:

Usage:


And there you have it, just a few lines of code in order to create or extract a .zip file!

Share on FacebookTweet about this on TwitterShare on Google+Share on StumbleUponShare on LinkedInShare on RedditPin on PinterestShare on TumblrDigg thisPrint this pageEmail this to someone

5 Responses to Create and extract .zip files in C#

  1. monica says:

    Muchas gracias por compartir!! funciono perfecto

  2. suraj kshirsagar says:

    my problem is i want to zip single file with in a folder .but not created zip file with that file anther empty zip file with same name is created

  3. tjchamaz says:

    This is wonderful, thanks!!!!

  4. Den mar says:

    Is this used as concole app, or with gui?

Leave a Reply

Your email address will not be published. Required fields are marked *