zoukankan      html  css  js  c++  java
  • C#,调用Process解压文件

    代码如下:
    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文件拷到项目所在路径

  • 相关阅读:
    java SSM 框架 多数据源 代码生成器 websocket即时通讯 shiro redis 后台框架源码
    PHP5.5.13 + Apache2.4.7安装配置流程详解
    mybatis-generator 自动生成mapper以及实体类
    spring cloud之Feign的使用
    spring cloud 初体验
    redis 分布式锁
    Activiti 工作流之所学所感(基本配置) DAY1
    druid 连接池加密算法
    ssm+redis整合(通过aop自定义注解方式)
    aop (权限控制之功能权限)
  • 原文地址:https://www.cnblogs.com/wuqihui/p/3328364.html
Copyright © 2011-2022 走看看