How to Zip the folder using C#
public static void AddFolderToZip(string sourcePath,string zipFullName)
{
DirectoryInfo CurrentFolder = new DirectoryInfo(sourcePath);
DirectoryInfo RootFolder = CurrentFolder.Parent;
DirectoryInfo[] SubFolders = RootFolder.GetDirectories(CurrentFolder.Name);
if (SubFolders.LongLength > 0)
{
//Add all subfolders to the zip if any
for (int i = 0; i < SubFolders.Length; i++)
{
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = "7za.exe";
if (!(new FileInfo(zipFullName).Exists))
{
p.Arguments = "a -tzip \"" + zipFullName + "\" \"" + SubFolders[0].FullName + "\" -mx=9";
}
else
{
p.Arguments = "u \"" + zipFullName + "\" \"" + SubFolders[i].FullName + "\" -mx=9";
}
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
//x.WaitForExit(500);
x.WaitForExit();
}
}
}
/* Calling a Method to zip the file using 7za.exe*/
string SourceFolder= ConfigurationManager.AppSettings.Get("SourceFolder");
string ZipFolderPath = ConfigurationManager.AppSettings.Get("ZipFolderPath");
ZipFolderPath= ZipFolderPath.EndsWith("\\") ? ZipFolderPath : ZipFolderPath + "\\";
ZipFolderPath= ZipFolderPath + "test.zip";
ZipHelper.AddFolderToZip(SourceFolder, ZipFolderPath);
{
DirectoryInfo CurrentFolder = new DirectoryInfo(sourcePath);
DirectoryInfo RootFolder = CurrentFolder.Parent;
DirectoryInfo[] SubFolders = RootFolder.GetDirectories(CurrentFolder.Name);
if (SubFolders.LongLength > 0)
{
//Add all subfolders to the zip if any
for (int i = 0; i < SubFolders.Length; i++)
{
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = "7za.exe";
if (!(new FileInfo(zipFullName).Exists))
{
p.Arguments = "a -tzip \"" + zipFullName + "\" \"" + SubFolders[0].FullName + "\" -mx=9";
}
else
{
p.Arguments = "u \"" + zipFullName + "\" \"" + SubFolders[i].FullName + "\" -mx=9";
}
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
//x.WaitForExit(500);
x.WaitForExit();
}
}
}
/* Calling a Method to zip the file using 7za.exe*/
string SourceFolder= ConfigurationManager.AppSettings.Get("SourceFolder");
string ZipFolderPath = ConfigurationManager.AppSettings.Get("ZipFolderPath");
ZipFolderPath= ZipFolderPath.EndsWith("\\") ? ZipFolderPath : ZipFolderPath + "\\";
ZipFolderPath= ZipFolderPath + "test.zip";
ZipHelper.AddFolderToZip(SourceFolder, ZipFolderPath);
Comments
Post a Comment