/// <summary>
/// 解压RAR和ZIP⽂件(需存在(只要⾃⼰电脑上可以解压或压缩⽂件就存在))
/// </summary>
/// <param name="UnPath">解压后⽂件保存⽬录</param>
/// <param name="rarPathName">待解压⽂件存放绝对路径(包括⽂件名称)</param>
/// <param name="IsCover">所解压的⽂件是否会覆盖已存在的⽂件(如果不覆盖,所解压出的⽂件和已存在的相同名称⽂件不会共同存在,只保留原已存在⽂件)</param>
/// <param name="PassWord">解压密码(如果不需要密码则为空)</param>
/// <returns>true(解压成功);false(解压失败)</returns>
public static bool UnRarOrZip(string UnPath, string rarPathName, bool IsCover,string PassWord)
{
if (!Directory.Exists(UnPath))
Directory.CreateDirectory(UnPath);
Process Process1 = new Process();
Process1.StartInfo.FileName = "";
Process1.StartInfo.CreateNoWindow = true;
string cmd = "";
if (!string.IsNullOrEmpty(PassWord) && IsCover)
//解压加密⽂件且覆盖已存在⽂件( -p密码 )
cmd = string.Format(" x -p{0} -o+ {1} {2} -y", PassWord, rarPathName, UnPath);
else if (!string.IsNullOrEmpty(PassWord) && !IsCover)
//解压加密⽂件且不覆盖已存在⽂件( -p密码 )
cmd = string.Format(" x -p{0} -o- {1} {2} -y", PassWord, rarPathName, UnPath);
else if (IsCover)
//覆盖命令( x -o+ 代表覆盖已存在的⽂件)
cmd = string.Format(" x -o+ {0} {1} -y" , rarPathName,UnPath);
else
//不覆盖命令( x -o- 代表不覆盖已存在的⽂件)
压缩文件怎么解压cmd = string.Format(" x -o- {0} {1} -y", rarPathName, UnPath);
//命令
Process1.StartInfo.Arguments = cmd;
Process1.Start();
Process1.WaitForExit();//⽆限期等待进程 退出
/
/Process1.ExitCode==0指正常执⾏,Process1.ExitCode==1则指不正常执⾏
if (Process1.ExitCode == 0)
{
Process1.Close();
return true;
}
else
{
Process1.Close();
return false;
}
}
/// <summary>
/// 压缩⽂件成RAR或ZIP⽂件(需存在(只要⾃⼰电脑上可以解压或压缩⽂件就存在))
/// </summary>
/// <param name="filesPath">将要压缩的⽂件夹或⽂件的绝对路径</param>
/// <param name="rarPathName">压缩后的压缩⽂件保存绝对路径(包括⽂件名称)</param>
/// <param name="IsCover">所压缩⽂件是否会覆盖已有的压缩⽂件(如果不覆盖,所压缩⽂件和已存在的相同名称的压缩⽂件不会共同存在,只保留原已存在压缩⽂件)</param> /// <param name="PassWord">压缩密码(如果不需要密码则为空)</param>
/// <returns>true(压缩成功);false(压缩失败)</returns>
public static bool CondenseRarOrZip(string filesPath, string rarPathName,bool IsCover, string PassWord)
{
string rarPath = Path.GetDirectoryName(rarPathName);
if (!Directory.Exists(rarPath))
Directory.CreateDirectory(rarPath);
Process Process1 = new Process();
Process1.StartInfo.FileName = "";
Process1.StartInfo.CreateNoWindow = true;
string cmd = "";
if (!string.IsNullOrEmpty(PassWord) && IsCover)
//压缩加密⽂件且覆盖已存在压缩⽂件( -p密码 -o+覆盖 )
cmd = string.Format(" a -ep1 -p{0} -o+ {1} {2} -r", PassWord, rarPathName, filesPath);
else if (!string.IsNullOrEmpty(PassWord) && !IsCover)
//压缩加密⽂件且不覆盖已存在压缩⽂件( -p密码 -o-不覆盖 )
cmd = string.Format(" a -ep1 -p{0} -o- {1} {2} -r", PassWord, rarPathName, filesPath);
else if (string.IsNullOrEmpty(PassWord) && IsCover)
//压缩且覆盖已存在压缩⽂件( -o+覆盖 )
cmd = string.Format(" a -ep1 -o+ {0} {1} -r", rarPathName, filesPath);
else
//压缩且不覆盖已存在压缩⽂件( -o-不覆盖 )
cmd = string.Format(" a -ep1 -o- {0} {1} -r", rarPathName, filesPath);
//命令
Process1.StartInfo.Arguments = cmd;
Process1.Start();
Process1.WaitForExit();//⽆限期等待进程 退出
//Process1.ExitCode==0指正常执⾏,Process1.ExitCode==1则指不正常执⾏
if (Process1.ExitCode == 0)
{
Process1.Close(); return true;
}
else
{
Process1.Close(); return false;
}
}
发布评论