c# winrar 打包

简介: 引用:http://hi.baidu.com/linrao/blog/item/00e245a5cc750ee29152ee71.html using System;using System.Collections.

引用:http://hi.baidu.com/linrao/blog/item/00e245a5cc750ee29152ee71.html

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.IO;
using System.Diagnostics;

namespace JaveSystem
{
    public class WinRar
    {
        static WinRar()
        {
            //判断是否安装了WinRar.exe
            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
            _existSetupWinRar = !string.IsNullOrEmpty(key.GetValue(string.Empty).ToString());

            //获取WinRar.exe路径
            _winRarPath = key.GetValue(string.Empty).ToString();
        }

        static bool _existSetupWinRar;
        /// <summary>
        /// 获取是否安装了WinRar的标识
        /// </summary>
        public static bool ExistSetupWinRar
        {
            get { return _existSetupWinRar; }
        }

        static string _winRarPath;
        /// <summary>
        /// 获取WinRar.exe路径
        /// </summary>
        public static string WinRarPath
        {
            get { return _winRarPath; }
        }

        /// <summary>
        /// 压缩到.rar
        /// </summary>
        /// <param name="intputPath">输入目录</param>
        /// <param name="outputPath">输出目录</param>
        /// <param name="outputFileName">输出文件名</param>
        public static void CompressRar(string intputPath, string outputPath, string outputFileName)
        {
            //rar 执行时的命令、参数
            string rarCmd;
            //启动进程的参数
            ProcessStartInfo processStartInfo;
            //进程对象
            Process process;
            try
            {
                if (!ExistSetupWinRar)
                {
                    throw new ArgumentException("Not setuping the winRar, you can Compress.make sure setuped winRar.");
                }
                //判断输入目录是否存在
                if (!Directory.Exists(intputPath))
                {
                    throw new ArgumentException("CompressRar'arge : inputPath isn't exsit.");
                }
                //命令参数
                rarCmd = " a " + outputFileName + " " + intputPath + " -r";
                //创建启动进程的参数
                processStartInfo = new ProcessStartInfo();
                //指定启动文件名
                processStartInfo.FileName = WinRarPath;
                //指定启动该文件时的命令、参数
                processStartInfo.Arguments = rarCmd;
                //指定启动窗口模式:隐藏
                processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                //指定压缩后到达路径
                processStartInfo.WorkingDirectory = outputPath;
                //创建进程对象
                process = new Process();
                //指定进程对象启动信息对象
                process.StartInfo = processStartInfo;
                //启动进程
                process.Start();
                //指定进程自行退行为止
                process.WaitForExit();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 解压.rar
        /// </summary>
        /// <param name="inputRarFileName">输入.rar</param>
        /// <param name="outputPath">输出目录</param>
        public static void UnCompressRar(string inputRarFileName, string outputPath)
        {
            //rar 执行时的命令、参数
            string rarCmd;
            //启动进程的参数
            ProcessStartInfo processStartInfo;
            //进程对象
            Process process;
            try
            {
                if (!ExistSetupWinRar)
                {
                    throw new ArgumentException("Not setuping the winRar, you can UnCompress.make sure setuped winRar.");
                }
                //如果压缩到目标路径不存在
                if (!Directory.Exists(outputPath))
                {
                    //创建压缩到目标路径
                    Directory.CreateDirectory(outputPath);
                }
                rarCmd = "x " + inputRarFileName + " " + outputPath + " -y";

                processStartInfo = new ProcessStartInfo();
                processStartInfo.FileName = WinRarPath;
                processStartInfo.Arguments = rarCmd;
                processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                processStartInfo.WorkingDirectory = outputPath;

                process = new Process();
                process.StartInfo = processStartInfo;
                process.Start();
                process.WaitForExit();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

RAR参数:

一、压缩命令
1、将temp.txt压缩为temp.rarrar a temp.rar temp.txt 
2、将当前目录下所有文件压缩到temp.rarrar a temp.rar *.* 
3、将当前目录下所有文件及其所有子目录压缩到temp.rarrar a temp.rar *.* -r 
4、将当前目录下所有文件及其所有子目录压缩到temp.rar,并加上密码123rar a temp.rar *.* -r -p123

二、解压命令
1、将temp.rar解压到c:\temp目录rar e temp.rar c:\temprar e *.rar c:\temp(支持批量操作) 
2、将temp.rar解压到c:\temp目录,并且解压后的目录结构和temp.rar中的目录结构一


压缩目录test及其子目录的文件内容 
Wzzip test.zip test -r -P 
WINRAR A test.rar test -r 

删除压缩包中的*.txt文件 
Wzzip test.zip *.txt -d 
WinRAR d test.rar *.txt 


刷新压缩包中的文件,即添加已经存在于压缩包中但更新的文件 
Wzzip test.zip test -f 
Winrar f test.rar test 


更新压缩包中的文件,即添加已经存在于压缩包中但更新的文件以及新文件 
Wzzip test.zip test -u 
Winrar u test.rar test 


移动文件到压缩包,即添加文件到压缩包后再删除被压缩的文件 
Wzzip test.zip -r -P -m 
Winrar m test.rar test -r 


添加全部 *.exe 文件到压缩文件,但排除有 a或b 
开头名称的文件 
Wzzip test *.exe -xf*.* -xb*.* 
WinRAR a test *.exe -xf*.* -xb*.* 


加密码进行压缩 
Wzzip test.zip test 
-s123。注意密码是大小写敏感的。在图形界面下打开带密码的压缩文件,会看到+号标记(附图1)。 
WINRAR A test.rar test -p123 
-r。注意密码是大小写敏感的。在图形界面下打开带密码的压缩文件,会看到*号标记(附图2)。 


按名字排序、以简要方式列表显示压缩包文件 
Wzzip test.zip -vbn 
Rar l test.rar 


锁定压缩包,即防止未来对压缩包的任何修改 
无对应命令 
Winrar k test.rar 


创建360kb大小的分卷压缩包 
无对应命令 
Winrar a -v360 test 


带子目录信息解压缩文件 
Wzunzip test -d 
Winrar x test -r 


不带子目录信息解压缩文件 
Wzunzip test 
Winrar e test 


解压缩文件到指定目录,如果目录不存在,自动创建 
Wzunzip test newfolder 
Winrar x test newfolder 


解压缩文件并确认覆盖文件 
Wzunzip test -y 
Winrar x test -y 


解压缩特定文件 
Wzunzip test *.txt 
Winrar x test *.txt 


解压缩现有文件的更新文件 
Wzunzip test -f 
Winrar x test -f 


解压缩现有文件的更新文件及新文件 
Wzunzip test -n 
Winrar x test -u 


批量解压缩文件 
Wzunzip *.zip 
WinRAR e *.rar

相关文章
|
C# C++
C#如何打包EXE程序生成setup安装文件
项目结束之后,有需要将winForm程序打包成.exe文件提供给用户。这里记录一下打包过程。
2452 0
C#如何打包EXE程序生成setup安装文件
|
小程序 C# Windows
|
C#
用C#开发类似QQ输入法的不规则窗体的程序详解+代码打包分享
QQ窗体是圆角的,Kugoo窗体也是圆角的,QQ的输入法皮肤窗体是不规则的。。。等N多日常使用的程序主窗体都不是四四方方的,其实都是调用2D绘画函数按指定的路径画出来的,那么这一课我给入门级学者讲解的是关于C#实现这一功能。
1225 0
|
Java C# 开发工具
C#使用Xamarin开发可移植移动应用进阶篇(8.打包生成安卓APK并精简大小),附源码
原文:C#使用Xamarin开发可移植移动应用进阶篇(8.打包生成安卓APK并精简大小),附源码 前言 系列目录 C#使用Xamarin开发可移植移动应用目录 源码地址:https://github.
1998 0
|
XML C# 图形学
Unity 使用VisualStuido将C#脚本封装打包DLL使用
为了方便开发,不破坏写好工具的封装性,我们经常讲C#脚本打包成DLL使用,操作流程如下 创建项目,注意选择Net Framework版本,过高Unity无法支持 添加在脚本中使用的unity引用,在封装的脚本中我只使用了UnityEngine.
2411 0
|
区块链 C# Windows