Published on: 29 January, 2016

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

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

Andre
03 August, 2017
Dude, at least copy paste your code directly from IDE. This waste of space is broken just looking at it.
andy
03 August, 2017
Hi Andre, sorry for the issue. i just removed the extra space. it might have been converted wrong when i copied from IDE to text editor. Probably next time, i will include the source code for easy reading.
Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.