C# 解压RAR压缩文件

/// 解压文件(不带密码) RAR压缩程序  返回解压出来的文件数量
       /// </summary>
       /// <param name="destPath">解压至目录</param>
       /// <param name="rarfilePath">压缩文件路径</param>
       public static int RARToFileEmail(string destPath, string rarfilePath)
       {
           try
           {
               //组合出需要shell的完整格式
               string shellArguments = string.Format("x -o+ \"{0}\" \"{1}\\\"",
                   rarfilePath, destPath);
 
               //用Process调用
               using (Process unrar = new Process())
               {
                   unrar.StartInfo.FileName = "winrar.exe";
                   unrar.StartInfo.Arguments = shellArguments;
                   //隐藏rar本身的窗口
                   unrar.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                   unrar.Start();
                   //等待解压完成
                   unrar.WaitForExit();
                   unrar.Close();
               }
               //统计解压后的目录和文件数
               //string str=string.Format("解压完成,共解压出:{0}个目录,{1}个文件",
               //    di.GetDirectories().Length, di.GetFiles().Length);
               //return str;
           }
           catch (Exception ex)
           {
               return 0;
           }
           DirectoryInfo di = new DirectoryInfo(destPath);
           int dirfileCount = 0;
           foreach (System.IO.DirectoryInfo dir in di.GetDirectories())
           {
               dirfileCount++;
           }
           foreach (System.IO.FileInfo item in di.GetFiles())
           {
               dirfileCount++;
           }
           return dirfileCount;
       }

 

posted @ 2013-11-01 20:08  Net-Spider  阅读(221)  评论(0)    收藏  举报