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文件拷到项目所在路径

  • 相关阅读:
    常用数据库种类 以及优缺点
    数据库
    Python中os与sys两模块的区别
    python操作数据库
    python——解释型语言
    面向对象的三大特性
    Android Fastboot 与 Recovery 和刷机 千山万水迷了鹿
    selenium与appium怎样联系
    Pycharm快捷键
    uiautomator python版本
  • 原文地址:https://www.cnblogs.com/wuqihui/p/3328364.html
Copyright © 2011-2022 走看看