博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C#,调用Process解压文件

Posted on 2013-09-18 14:26  Wuqh  阅读(448)  评论(0编辑  收藏  举报
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics;
using System.IO;

namespace IHAP.Infrastructure.Utility
{
    public class RarProcessor
    {
        /// <summary>
        /// 解压RAR文件
        /// </summary>
        /// <param name="extractPath">解压路径</param>
        ///<param name="FileFullPath">需要解压的文件,带文件名的完整路径</param>
        ///<param name="rarFullPath">RAR.exe文件的完整路径</param>
        ///<param name="isDeleteFile">是否删除文件</param>
        /// <returns></returns>
        public static bool ExtractRar(string extractPath, string FileFullPath, string rarFullPath, bool isDeleteFile)
        {
            string rarExeFullPath = string.Empty;
            string extractFullPath = string.Empty;
            string startPath = AppDomain.CurrentDomain.BaseDirectory;
            if (string.IsNullOrEmpty(rarFullPath))
            {
                //程序集启动路径
                rarExeFullPath = Path.Combine(startPath, @"Rar.exe");
            }
            else
            {
                rarExeFullPath = rarFullPath;
            }

            if (!File.Exists(FileFullPath))
            {
                //判断需要解压的文件存不存
                throw new Exception(string.Format("需要解压的{0}不存在", FileFullPath));
            }

            //解压的路径
            if (string.IsNullOrEmpty(extractPath))
            {
                extractFullPath = startPath;
            }
            else
            {
                extractFullPath = extractPath;
            }


            if (!File.Exists(rarExeFullPath))
            {
                //RAR.EXE不存在,抛出错误
                throw new Exception(string.Format("Rar.exe不存在!路径为:{0}", rarExeFullPath));
            }

            System.Diagnostics.Process process = new Process();
            process.StartInfo.FileName = rarExeFullPath;
            //这里注意,解压路径不能带有空格,因为Dos分别不出空格,误以为是另外参数,所以用双引号
            process.StartInfo.Arguments = " x -inul -y " + string.Format(" \"{0}\" \"{1}\"", FileFullPath, extractFullPath);
            process.Start();//解压开始  
            while (!process.HasExited)            //等待解压的完成  
            {
            }

            if (isDeleteFile)
            {
                File.Delete(FileFullPath);
            }

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static bool CompressRar(string filePath)
        {
            return false;
        }

    }
}
详细代码

这里压缩方法没有实现

方便部署,最好就把RAR.exe文件拷到项目所在路径