November 16, 2009

Zip files and folders in .NET




A lot of solutions exists that show how to zip files in .NET, for example some of them using SharpZipLib, GZipStream, Windows Shell API or the ZipLibrary class. However one lesser known solution which I have found to be very useful is the DotNetZip library

As given on the site - “DotNetZip is an easy-to-use FREE class library and toolset for manipulating zip files or folders. Zip and Unzip is easy: with DotNetZip, .NET applications written in VB, C# - any .NET language - can easily create, read, extract, or update zip files. It works on Mono or MS .NET”

You can download DotNetZip Library over here.

Here’s a sample that shows how to use this library to zip all files in a folder

C#

using System;
using Ionic.Zip;

namespace ZipSample
{
class Program
{
static void Main(string[] args)
{
try
{
using (ZipFile zip = new ZipFile())
{
zip.AddDirectory(@"D:\Ajax");
zip.Save(@"D:\Ajax\Ajax.zip");
Console.WriteLine("Files zipped");
}
}
catch (ZipException ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}

VB.NET

Imports System
Imports Ionic.Zip

Namespace ZipSample
Class Program
Shared Sub Main(ByVal args() As String)
Try
Using
zip As New ZipFile()
zip.AddDirectory("D:\Ajax")
zip.Save("D:\Ajax\Ajax.zip")
Console.WriteLine("Files zipped")
End Using
Catch
ex As ZipException
Console.WriteLine(ex.Message)
End Try
Console.ReadLine()
End Sub
End Class
End Namespace
Depending on the size of the files in a folder, it may take some time for the zip operation to complete.

'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

0 Responses to "Zip files and folders in .NET"
 

Copyright © 2009-2012 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions