Skip to content

Instantly share code, notes, and snippets.

@yemrekeskin
Created August 24, 2013 14:22
Show Gist options
  • Save yemrekeskin/6328427 to your computer and use it in GitHub Desktop.
Save yemrekeskin/6328427 to your computer and use it in GitHub Desktop.
ZipHelper with Ionic.Zip ( http://www.nuget.org/packages/DotNetZip )
public static class ZipHelper
{
public readonly static string ZipPassword = ConfigurationManager.AppSettings["ZipPassword"].ToString();
public static bool Zip(string filePath,string TargetDirectory)
{
if (File.Exists(filePath))
throw new ApplicationException("");
if (Directory.Exists(TargetDirectory))
throw new ApplicationException("");
bool rv = false;
try
{
using (ZipFile zip = new ZipFile())
{
zip.Password = ZipPassword;
zip.Comment = "";
zip.AddFile(filePath, "Files");
zip.Save(TargetDirectory);
}
}
catch (Exception ex)
{
rv = false;
}
return rv;
}
public static bool Zip(string[] fileNames,string TargetDirectory)
{
bool rv = false;
try
{
for (int i = 0; i < fileNames.Count(); i++)
{
Zip(fileNames[i], TargetDirectory);
}
}
catch (Exception ex)
{
rv = false;
}
return rv;
}
public static bool UnZip(string TargetDirectory,string filePath)
{
bool rv = false;
try
{
using (ZipFile zip=ZipFile.Read(filePath))
{
foreach (ZipEntry e in zip)
{
e.ExtractWithPassword(TargetDirectory, ZipPassword);
}
}
}
catch (Exception ex)
{
rv = false;
}
return rv;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment