C# 文件压缩与解压

压缩部分:
         try
            {
                string Rarexe = @"C:\Program Files\WinRAR\WinRAR.exe"; 
                //WinRAR.exe 的完整路径
                string mulu = @"F:\仓存数据\";
                // 子文件所在目录
                string fileList =null;

                fileList += (mulu + "NewDB.xlsx ");

                fileList += (mulu + "出入库总表.xlsx ");

                fileList += (mulu + "A11-台账.xlsx ");

                fileList += (mulu + "A10-台账.xlsx ");

                fileList += (mulu + "A08-台账.xlsx ");

                fileList += (mulu + "A05-台账.xlsx ");
                // 组织压缩文件列表
                string Cmd = string.Format("a " + DateTime.Now.ToString() + " {0} -r -ibck -inul",fileList); 
                //WinRAR 命令参数 
                ProcessStartInfo Startinfo = new ProcessStartInfo();
                Startinfo.FileName = Rarexe;
                // 设置压缩主程序位置
                Startinfo.Arguments = Cmd;                          
                // 设置命令参数  
                Startinfo.WorkingDirectory = @"F:\仓存数据\备份\7\";
                // 文件保存路径
                Process Process = new Process();
                // 创建新进程
                Process.StartInfo = Startinfo;
                // 给进程传递所有参数
                Process.Start();
                // 启动进程
                Process.WaitForExit(); 
                // 无限期等待进程 winrar.exe 退出      
                Process.Close();
                // 释放所有占用的资源
                MessageBox.Show("压缩完成!");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

解压部分:
        try
            {
                string Rarexe = @"C:\Program Files\WinRAR\WinRAR.exe";
                //WinRAR.exe 的完整路径
                string jypath = @"C:\";
                // 解压存放的路径
                Directory.CreateDirectory(jypath);
                // 创建指定路径的文件夹
                string Cmd = string.Format("x {0} {1} -ibck -inul ", @"F:\仓存数据\2014-7-10.rar", jypath);
                //WinRAR 命令参数 
                ProcessStartInfo Startinfo = new ProcessStartInfo();
                Startinfo.FileName = Rarexe;
                // 设置压缩主程序位置
                Startinfo.Arguments = Cmd;
                // 设置命令参数  
                Process Process = new Process();
                // 创建新进程
                Process.StartInfo = Startinfo;
                // 给进程传递所有参数
                Process.Start();
                // 启动进程
                Process.WaitForExit();
                // 无限期等待进程 winrar.exe 退出      
                Process.Close();
                // 释放所有占用的资源
                MessageBox.Show("成功解压到:" + jypath);
            }
            catch (Exception em)
            {
                MessageBox.Show(em.Message);
            }

其他参数:

             * <命令> -<开关1> -<开关N> <压缩文件 > <文件...> <@列表文件...> <解压路径\> 
             *压缩 a      a -as -ed -inul -afrar -df -ibck -m4 -mt3 -or -y -hp123 d:\aa d:\aa.txt 
             *解压 x      x -hp123 -ibck -inul -y -mt5 d:\aa.rar a:\ 
             *a d:\Info.zip D:\easyui 
             *-af 指定格式 -afzip -afrar 
             *-as 在当前添加的文件列表中不存在的被压缩文件,将会从压缩文件中删除 
             *-df 压缩后删除源文件 
             *-dr 删除到回收站 
             *-ed 不添加空文件夹 
             *-hp 添加密码 -hp123456 
             *-ibck 后台运行 
             *-inul 禁止错误信息 
             *-loff 压缩完成后 关闭电源 
             *-m0 存储 添加文件到压缩文件但是不压缩   
             *-m1 最快 最快速的方法 ( 最低的压缩比)  
             *-m2 快速 快速压缩方法  
             *-m3 标准 标准 (默认 ) 压缩方法  
             *-m4 较好 较好的压缩方法 (较高的压缩比)  
             *-m5 最优 最优的压缩方法 (最高压缩比但是速度也最慢) 
             *-mtN 线程 -mt5 1~32 
             *-or 自动重命名文件 
             *-r 连同子文件 
             *-z 压缩后测试文件 
             *-y 所有弹窗选择"是" 
            
可以直接将WinRAR.exe 拷贝到 根目录下运行
不弹窗只需要打开开光 -ibck -inul 即可
沉浸在编程的世界之中,我们不是搬砖工,我是上帝之手。
原文地址:https://www.cnblogs.com/week/p/3967248.html