调用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);
   }
  }
   

 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值