A Console application to zip the folders present in a particular folder and again zip the zipped folder.
This small application is very useful in scenarios when one want to archive project files periodically. And where zipping of files take lots of time if done manually.
Create a console application. And name it as zip_utility. Specify the path you want to save it in. By default it will be in MyDocuments\VisualStudioProjects.
Include the following namespaces to the class…
using System.Text;
using System.IO;
using System.Collections;
using System.Diagnostics;
using System.Configuration;
using System.Threading;
using System.Windows.Forms;
Next step will be to write down the method which zips the folders. Lets call it as “Zipme()”.It first deletes all the .zip files that are already present in the particular folder,so that there is no confusion between already existing and new files,and again zip the folders afresh.then you can copy the resulting folder where the backup is kept.This tool is basically used to take the backups of the project folders at regular intervals. It becomes quiet cumbersome to zip each folder one at a time. It takes time and also monitoring for quiet sometime. By this tool we are intending to reduce the mentioned efforts.
It will be using winzip 9.0 with commandline support.
We will also be using a config file called “App.config” which will serve the purpose of setting some values.
Now add following class
private static void Zipme()
{
// “ZipFolder” is used as the key in the App.config
// FolderN is being set to the value assigned to the ZipFolder in App.config
string FolderN= ConfigurationSettings.AppSettings["ZipFolder"].ToString();
try
{
EmptyFolder(FolderN); //call to delete existing .zip files
//region defined to compress the subfolders.
#region for subdirectories
string[] dirs = Directory.GetDirectories(FolderN); //get info of subdirectories in FolderN
foreach(string dir in dirs)
{
Process cmd1 = new Process();
string path = Path.GetFullPath(dir);
cmd1.StartInfo.FileName = "cmd.exe";
cmd1.StartInfo.RedirectStandardInput = true;
cmd1.StartInfo.RedirectStandardOutput = true;
cmd1.StartInfo.CreateNoWindow = false;
cmd1.StartInfo.UseShellExecute = false;
cmd1.Start();
// command with parameters to compress
string strPath = @"C: && cd C:\Program Files\WinZip && WZZIP -whs -rP"+" "+ "\"" + dir+".zip"+ "\"" + " "+ "\"" + path+"\\*.*" + "\"" ;
cmd1.StandardInput.WriteLine(strPath);
Thread.Sleep(500);
// since using commandline support, it needs user to press a key as soon as one folder is zipped to continue for next,hence sending "ENTER"
SendKeys.SendWait("{ENTER}");
cmd1.StandardInput.Flush();
cmd1.StandardInput.Close();
Console.WriteLine(cmd1.StandardOutput.ReadToEnd());
}
#endregion for subdirectories
#region Directory Compression
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput =true;
cmd.StartInfo.CreateNoWindow = false;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
string zipCmd = @"C: && cd C:\Program Files\WinZip && WZZIP";
string strSep = " ";
// naming the folder with the date of creation appended to the folder name
string strPathDest = ConfigurationSettings.AppSettings["FinalZipFolder"].ToString() + "\\" + ConfigurationSettings.AppSettings["FinalZipName"].ToString() + DateTime.Now.ToShortDateString().Replace(@"/", "-") + @".ZIP";
string strPathSource = ConfigurationSettings.AppSettings["FinalZipFolder"].ToString() + @"\*.ZIP";
string strPath1 = zipCmd + strSep +"\""+ strPathDest+"\"" + strSep +"\""+ strPathSource+"\"";
cmd.StandardInput.WriteLine(strPath1);
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
Console.WriteLine(cmd.StandardOutput.ReadToEnd());
#endregion Directory Compression
}
catch(Exception e)
{
Console.WriteLine(e.Message);
string wait = Console.ReadLine();
}
}
add a web configuration file solution explorer -> add -> new item -> web configuration file and name it App.config file and put following code in appsettings tag
put each line in angle brackets with "/" before bracket close for each line. since having problem in publishing this code..
add key="ZipFolder" value="D:\zip_folder"
add key="FinalZipFolder" value="D:\zip_folder"
add key="FinalZipName" value="zip"
February 9, 2009
Console Application to zip your files
Subscribe to:
Posts (Atom)