CBreeze CBreeze - 1 year ago 54
C# Question

Zip a Folder Created in C#

I'm creating a folder in C# and I'm hoping to zip it up as soon as I've created it. I've had a look around (How to zip a folder), (http://dotnetzip.codeplex.com/) but no luck so far. I'm a bit apprehensive of using dotnetzip as it's last release was 5 years ago.

Is dotnetzip still relevant in Visual Studio 2015 or is there a more modern way of zipping folders in C# without using a package?

This is how I'm copying the folders;

private static void CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting)
{

SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\";
DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";

if (Directory.Exists(SourcePath))
{
if (Directory.Exists(DestinationPath) == false)
Directory.CreateDirectory(DestinationPath);

foreach (string fls in Directory.GetFiles(SourcePath))
{
FileInfo flinfo = new FileInfo(fls);
flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting);
}
foreach (string drs in Directory.GetDirectories(SourcePath))
{
DirectoryInfo drinfo = new DirectoryInfo(drs);
CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting);
}
}
}


I'm looking to zip the created folder after this.

Answer Source

To zip a folder, the .Net 4.5 framework contains ZipFile.CreateFromDirectory:

string startPath = @"c:\example\start";
string zipPath = @"c:\example\result.zip";

ZipFile.CreateFromDirectory(startPath, zipPath);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download