Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can any one give me idea about how to create a zip file of a folder(contain multiple sub folders)using c#.net.
Is there any inbuilt assembly in dot net or any other?
Posted
Updated 27-Jun-12 0:22am
v2

1. DON'T SHOUT!
2. I use ICSharpCode.SharpZipLib which can be freely downloaded and also used in commercial applications.
Code looks like that:
C#
foreach (string folderToZip in foldersToZip)
{
    if (!string.IsNullOrWhiteSpace(folderToZip) && Directory.Exists(folderToZip))
    {
        string targetFile = Path.Combine(targetFolder, Path.GetFileName(folderToZip) + ".zip");
        ZipPath(targetFile, folderToZip, null, true, null);
    }
}

and
C#
public static void ZipPath(string zipFilePath, string sourceDir, string pattern, bool withSubdirs, string password)
{
    FastZip fz = new FastZip();
    if (password != null)
        fz.Password = password;

    fz.CreateZip(zipFilePath, sourceDir, withSubdirs, pattern);
}
 
Share this answer
 
Comments
Vani Kulkarni 27-Jun-12 6:23am    
Clear and concise!
Sandeep Mewara 27-Jun-12 10:01am    
5!
StianSandberg 25-Apr-13 2:42am    
perfect. 5'd
Kumar Tanmay 30-Dec-14 5:19am    
Hi...this code is working fine....but on extracting the zip file it does not give the folder but files instead.
Hi
You can download this ICSharpCode.SharpZipLib.dll.

C#
FastZip fz = new FastZip();
fz.ExtractZip(Pass Path here);
 
Share this answer
 
Above solution is Good but still have some doubts in it.

we have find a better solution to zip and unzip files and folders using asp.net with c#, vb.net.
and we also fixed the problem which creates two folder when we unzip any folder.

we hope this article may help you in better way. you can also download source code, dll from this link.

DotNetZip – Zip and Unzip in C#, VB, any .NET language


Regards-
Dotnet Developers to Developers Group
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900