ISSUE :
 
Profile photo
Submitted by Dave Smith
on July 13th, 2011 at 11:54:34 EST

How to create compressed Zip file using C#?

I am trying to compress and Zip my deployment files using C#. Is there any way to Zip files using C# in .NET? I am also looking for zip compression for other part of my application where user upload and I zip the uploaded files. Any help please.


Profile photo
Replied by RaKumar
on July 13th, 2011 at 12:06:42 EST
You can create zip files using .NET built-in class ZipPackage. ZipPackage is part of System.IO.Packaging namespace. ZipPackage is actually inherited from Package class. You could actually use Package class because defailt compression for Package class is actually zip. This is added with .NET 3.0 to create zip files. System.IO.Packaging namespace is in assembly WindowsBase (in WindowsBase.dll). So you would need to add reference to WindowsBase assembly before using this ZipPackage.

Here is the code sample.
private void button1_Click(object sender, EventArgs e)
{
//On button click event, adding files to Zip file.
AddToZipFile("Output.zip", @"C:\Windows\Notepad.exe");
AddToZipFile("Output.zip", @"C:\Windows\System32\Calc.exe");
}

//Method to add file into Zip file
private static void AddToZipFile(string zipFilename, string filenameToAdd)
{
using (Package zipPackage = ZipPackage.Open(zipFilename, FileMode.OpenOrCreate))
{
string destFilename = ".\\" + Path.GetFileName(filenameToAdd);

Uri zipPartUri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));

if (zipPackage.PartExists(zipPartUri))
{
zipPackage.DeletePart(zipPartUri);
}

PackagePart zipPackagePart = zipPackage.CreatePart(zipPartUri, "", CompressionOption.Normal);

using (FileStream fileStream = new FileStream(filenameToAdd, FileMode.Open, FileAccess.Read))
{
using (Stream dest = zipPackagePart.GetStream())
{
CopyStream(fileStream, dest);
}
}
}
}

//Copy one stream data to another
private static void CopyStream(Stream source, Stream target)
{
const int bufSize = 0x1000;
byte[] buf = new byte[bufSize];
int bytesRead = 0;
while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
target.Write(buf, 0, bytesRead);
}


For more details:
http://msdn.microsoft.com/en-us/library/system.io.packaging.zippackage.aspx
Accepted Answer
Accepted
 Answer

Profile photo
Replied by Bikram Shrestha
on July 13th, 2011 at 12:56:49 EST
Use DotNetZip component. Its free open source component and easy to use.
http://dotnetzip.codeplex.com/

Profile photo
Replied by Jadwiga Watson
on July 13th, 2011 at 13:04:03 EST
Here is another free zip component - SharpZipLib. However it has some exception with licensing. So, if you don't have issue with their licensing, its easy to use as well.
http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx

Profile photo
Replied by Dave Smith
on July 13th, 2011 at 14:55:12 EST
All great solutions but I accepted the one without any 3rd party component. Thanks all.

Profile photo
Replied by Priya B
on July 16th, 2012 at 01:19:06 EST
I will say that more than the generic ZipPackage class and the new System.IO.Compression class of .NET 4.5, DotNetZip is better to use. Though it is a third party library it is 100% dotnet managed code and flawless. From my experience on using all the three, I found DotNetZip to be the best working library.

Check this out http://programmingfree.blogspot.in/2012/07/create-analyze-and-manipulate-zip-files.html

Profile photo
Replied by Muhammad Mohsin
on September 7th, 2013 at 06:17:57 EST
how to add reference from dotnet ?