45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 电脑教程 > 阅读资讯:如何调用Winrar.exe压缩和解压缩?

如何调用Winrar.exe压缩和解压缩?

2016-08-27 19:56:29 来源:www.45fan.com 【

如何调用Winrar.exe压缩和解压缩?

/// <summary>
/// 调用Winrar.exe压缩和解压缩。
/// </summary>
class Winrar
{
string winrarExe;
string password;

/// <summary>
/// 构造函数。
/// </summary>
/// <param name="directoryOfWinrarExe">Winrar.exe所在的目录(绝对路径)。请你拷贝Winrar.exe到一个你有读写权限的目录,然后用此目录作为参数。否则将不能执行。</param>
/// <param name="password">用来压缩或解压缩时使用的密码,如果没有密码,请用另一无password参数的构造函数。</param>
public Winrar(string directoryOfWinrarExe, string password)
{
directoryOfWinrarExe = CheckDirectoryName(directoryOfWinrarExe);
this.winrarExe = directoryOfWinrarExe + "Winrar.exe";

this.password = CheckPassword(password);
}

/// <summary>
/// 构造函数
/// </summary>
/// <param name="directoryOfWinrarExe">Winrar.exe所在的目录(绝对路径)。请你拷贝Winrar.exe到一个你有读写权限的目录,然后用此目录作为参数。否则将不能执行。</param>
public Winrar(string directoryOfWinrarExe)
{
directoryOfWinrarExe = CheckDirectoryName(directoryOfWinrarExe);
this.winrarExe = directoryOfWinrarExe + "Winrar.exe";

this.password = null;
}

/// <summary>
/// 检查目录名是否符合格式。
/// </summary>
/// <param name="directoryName">目录名(绝对路径)</param>
/// <returns>目录名(绝对路径)</returns>
static string CheckDirectoryName(string directoryName)
{
if (directoryName == null)
{
throw new Exception("目录名不可以为null。");
}

Regex regex = new Regex(@"^[a-zA-Z]:[^/n]*//$");
if (!regex.IsMatch(directoryName))
{
throw new Exception(@"目录名称请用绝对路径,须以“/”结束。如:C:/Windows/。");
}

return directoryName;
}

/// <summary>
/// 检查密码是否符合格式。
/// </summary>
/// <param name="password">密码</param>
/// <returns>密码</returns>
static string CheckPassword(string password)
{
if (password == null)
{
throw new Exception("密码不可以为null。如不要密码,请用另一无password参数的构造函数。");
}

Regex regex = new Regex(@"^/w+$");
if (!regex.IsMatch(password))
{
throw new Exception("密码请用字母,数字,下画线。");
}

return password;
}

/// <summary>
/// 检查是否扩展名为.rar的文件名。
/// </summary>
/// <param name="rarFile">winrar文件名(绝对路径)</param>
/// <returns>winrar文件名(绝对路径)</returns>
static string CheckRarFile(string rarFile)
{
if (rarFile == null)
{
throw new Exception("文件名不可以为null。");
}

Regex regex = new Regex(@"^[a-zA-Z]://.+/.rar$");
if (!regex.IsMatch(rarFile))
{
throw new Exception(@"文件名称请用绝对路径,须以“扩展名.rar”结束。如:C:/Windows/aa.rar");
}

return rarFile;
}

/// <summary>
/// 生成一个临时文件(backup.lst)。该文件中每一行包括一个要压缩的文件名。该文件名用于构造命令winrar a backup.rar @backup.rar。请参考winrar.exe帮助
/// </summary>
/// <param name="filesToCompress">要压缩的文件名数组。</param>
/// <param name="lstFile">临时文件名,用于构造命令winrar a backup.rar @backup.rar</param>
void CreateLstFile(string[] filesToCompress, out string lstFile)
{
string path = winrarExe.Substring(0, winrarExe.LastIndexOf("/") + 1);
string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".lst";
lstFile = path + filename;

StreamWriter sw = new StreamWriter(path + filename, false);

foreach (string file in filesToCompress)
{
sw.WriteLine(file);
}

sw.Close();
}

/// <summary>
/// 检查是文件名是否符合格式(绝对路径)。
/// </summary>
/// <param name="files">文件名数组(绝对路径)。</param>
static void CheckFiles(string[] files)
{
Regex regex = new Regex(@"[a-zA-Z]://.+/..+");

foreach (string file in files)
{
if (file == null)
{
throw new Exception("文件名不可以为null。");
}

if (!regex.IsMatch(file))
{
throw new Exception(@"文件名称请用绝对路径,并注意格式。如:C:/Windows/aa.txt");
}
}

}

/// <summary>
/// 压缩指定目录。
/// </summary>
/// <param name="directoryToCompress">要压缩的目录,包括其下的子目录。</param>
/// <param name="fileToSave">压缩后的文件名.</param>
/// <param name="deleteDirectoryToCompress">压缩后是否删除源目录。</param>
public void Compress(string directoryToCompress, string rarFileToSave, bool deleteDirectoryToCompress)
{
directoryToCompress = CheckDirectoryName(directoryToCompress);
rarFileToSave = CheckRarFile(rarFileToSave);

if (File.Exists(rarFileToSave))
{
File.Delete(rarFileToSave);
}

Process p = new Process();
// 需要启动的程序名
p.StartInfo.FileName = winrarExe;
// 参数
string arguments = @"a -ep1 -r -o+ -inul -y";
if (password != null)
{
arguments += " -p" + password;
}
arguments += " " + rarFileToSave + " " + directoryToCompress;
p.StartInfo.Arguments = arguments;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();//启动
while (!p.HasExited)
{
}
p.WaitForExit();
if (deleteDirectoryToCompress)
{
Directory.Delete(directoryToCompress, true);
}
}

/// <summary>
/// 压缩指定文件列表。
/// </summary>
/// <param name="rarFileToSave">压缩后的文件名(绝对路径)。</param>
/// <param name="deleteSourceFiles">压缩后是否删除源文件列表。</param>
/// <param name="filesToCompress">要压缩的文件列表(绝对路径)。</param>
public void Compress(string rarFileToSave, bool deleteSourceFiles, params string[] filesToCompress)
{
rarFileToSave = CheckRarFile(rarFileToSave);
if (File.Exists(rarFileToSave))
{
File.Delete(rarFileToSave);
}

CheckFiles(filesToCompress);

string lstFile;
this.CreateLstFile(filesToCompress, out lstFile);


Process p = new Process();
// 需要启动的程序名
p.StartInfo.FileName = winrarExe;
// 参数
string arguments = @"a -ep1 -r -o+";
if (password != null)
{
arguments += " -p" + password;
}
arguments += " " + rarFileToSave + " @" + lstFile;
p.StartInfo.Arguments = arguments;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();//启动
while (!p.HasExited)
{
}
p.WaitForExit();

if (deleteSourceFiles)
{
foreach (string file in filesToCompress)
File.Delete(file);
}

File.Delete(lstFile);
}

/// <summary>
/// 解压缩指定的rar文件。
/// </summary>
/// <param name="rarFileToDecompress">rar文件(绝对路径)。</param>
/// <param name="directoryToSave">解压缩保存的目录。</param>
/// <param name="deleteRarFile">解压缩后删除rar文件。</param>
public void Decompress(string rarFileToDecompress, string directoryToSave, bool deleteRarFile)
{
directoryToSave = CheckDirectoryName(directoryToSave);
rarFileToDecompress = CheckRarFile(rarFileToDecompress);

Process p = new Process();
// 需要启动的程序名
p.StartInfo.FileName = winrarExe ;
// 参数
string arguments = @"x -inul -y -o+";

if (password != null)
{
arguments += " -p" + password ;
}
arguments += " " + rarFileToDecompress + " " + directoryToSave;

p.StartInfo.Arguments = arguments;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();//启动
while(!p.HasExited)
{
}
p.WaitForExit();

if (deleteRarFile)
{
File.Delete(rarFileToDecompress);
}
}

}

 

本文地址:http://www.45fan.com/dnjc/68576.html
Tags: 压缩 summary Winrar.exe
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部