Published on: 29 January, 2016

How to use open source SharpZipLib library for zipping files in .Net

Posted by andy | 1695 views 0 likes 0 favourites 0 comments
Tumblr Digg Google Plus

There are couple open source zip library available for .Net framework. One of the best I can recommend is SharpZipLibrary, it is pretty easy to use and reliable. In this tutorial, I will show you how easily you can zip a folder that have multiple sub folders and files.

Firstly you will need to download the dll component this ISharpCode site. Once download is completed, you can add this dll reference into your project, either it can be MVC site, ASP.Net site, console or windows application.

In your header code, please import the following library namespace. Note: the System.IO is part of .Net framework.

using System.IO;
using ICSharpCode.SharpZipLib.Zip;

Next we need to create a function method that will zip a folder and perform recursive zipping inside of a folder

public static void ZipFolder(string CurrentFolder, ZipOutputStream zStream)
{
	string[] SubFolders = Directory.GetDirectories(CurrentFolder);

	//calls the method recursively for each subfolder
	foreach (string Folder in SubFolders)
	{
		ZipFolder(CurrentFolder, Folder, zStream);
	}

	string relativePath = CurrentFolder.Substring(CurrentFolder.Length) + "/";

	//the path "/" is not added or a folder will be created
	//at the root of the file
	if (relativePath.Length > 1)
	{
		ZipEntry dirEntry;
		dirEntry = new ZipEntry(relativePath);
		dirEntry.DateTime = DateTime.Now;
	}

	//adds all the files in the folder to the zip
	foreach (string file in Directory.GetFiles(CurrentFolder))
	{
		AddFileToZip(zStream, relativePath, file);
	}
}

The next function is to create a method that will add a file to a zip object.

private static void AddFileToZip(ZipOutputStream zStream, string relativePath, string file)
{
	byte[] buffer = new byte[4096];

	//the relative path is added to the file in order to place the file within
	//this directory in the zip
	string fileRelativePath = (relativePath.Length > 1 ? relativePath : string.Empty)
							  + Path.GetFileName(file);

	ZipEntry entry = new ZipEntry(fileRelativePath);
	entry.DateTime = DateTime.Now;
	zStream.PutNextEntry(entry);

	using (FileStream fs = File.OpenRead(file))
	{
		int sourceBytes;
		do
		{
			sourceBytes = fs.Read(buffer, 0, buffer.Length);
			zStream.Write(buffer, 0, sourceBytes);
		} while (sourceBytes > 0);
	}
}

This is how you will use the function method to zip a folder.

ZipOutputStream zip = new ZipOutputStream(File.Create("c://sample.zip"));
zip.SetLevel(9);
ZipFolder("c://sample", zip);
zip.Finish();
zip.Close();
Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.
Related Articles
Published on: 13 February, 2017
One thing to remember when publishing your ASP.Net web applications, you may find out, some files could be not completely published due to the settings on the file properties. One example that I had are some font types file. In order to fix this problem you can navigate to the files that are missing and right click the properties.
Published on: 11 May, 2016
The slidingExpiration attribute is used in ASP.Net form authentication. This property is used to reset the expiration time for a valid authentication cookie if a request that has already being made has passed the timeout interval. Which means, if it is already expires, the current user will require to re-authentication again.
Related Tutorials