IMG-LOGO

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

andy - 29 Jan, 2016 9177 Views 4 Comment

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 Aug, 2017
Dude, at least copy paste your code directly from IDE. This waste of space is broken just looking at it.
andy
03 Aug, 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.
amy
13 Sep, 2019
hi, what about unzipping, into files and target folders
Durga
12 Sep, 2021
Very useful share.... It's Working... Thank you so much...
Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.

Related Articles

How to remove html tags from string in c#?

Sometimes you need to remove HTML tags from string to ensure there are no dangerous or malicious scripts especially when you want to store the string or data text into the database.