zoukankan      html  css  js  c++  java
  • c# 压缩文件

    引用: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

  • 相关阅读:
    解决:Could not resolve archetype org.apache.maven.archetypes
    Spring MVC配置MyBatis输出SQL
    Spring集成MyBatis 通用Mapper以及 pagehelper分页插件
    关于SpringMVC或Struts2接受参数接收不到的原因
    配置quartz启动时就执行一次
    ajaxFileUpload进行文件上传时,总是进入error
    spring mvc注入配置文件里的属性
    java中将一个文件夹下所有的文件压缩成一个文件
    flume failed to start agent because dependencies were not found in classpath
    ubuntu不能安装pip unable to install pip in unbuntu
  • 原文地址:https://www.cnblogs.com/sode/p/2414285.html
Copyright © 2011-2022 走看看