c# 压缩文件

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

相关文章
|
11月前
|
存储 监控 算法
基于 C# 的局域网计算机监控系统文件变更实时监测算法设计与实现研究
本文介绍了一种基于C#语言的局域网文件变更监控算法,通过事件驱动与批处理机制结合,实现高效、低负载的文件系统实时监控。核心内容涵盖监控机制选择(如事件触发机制)、数据结构设计(如监控文件列表、事件队列)及批处理优化策略。文章详细解析了C#实现的核心代码,并提出性能优化与可靠性保障措施,包括批量处理、事件过滤和异步处理等技术。最后,探讨了该算法在企业数据安全监控、文件同步备份等场景的应用潜力,以及未来向智能化扩展的方向,如文件内容分析、智能告警机制和分布式监控架构。
268 3
基于 C# 编写的 Visual Studio 文件编码显示与修改扩展插件
基于 C# 编写的 Visual Studio 文件编码显示与修改扩展插件
408 9
|
存储 C#
【C#】大批量判断文件是否存在的两种方法效率对比
【C#】大批量判断文件是否存在的两种方法效率对比
519 1
|
监控 安全 C#
使用C#如何监控选定文件夹中文件的变动情况?
使用C#如何监控选定文件夹中文件的变动情况?
458 19
|
编译器 C# Windows
C#基础:手动编译一个.cs源代码文件并生成.exe可执行文件
通过上述步骤,应该能够高效准确地编译C#源代码并生成相应的可执行文件。此外,这一过程强调了对命令行编译器的理解,这在调试和自动化编译流程中是非常重要的。
1647 2
|
文字识别 C# Python
使用C#将几个Excel文件合并去重分类
使用C#将几个Excel文件合并去重分类
247 3
|
存储 C#
C#使用哈夫曼编码实现压缩与解压
C#使用哈夫曼编码实现压缩与解压
200 0
|
XML 存储 缓存
C#使用XML文件的详解及示例
C#使用XML文件的详解及示例
820 0
【C#】C#读写Excel文件
【C#】C#读写Excel文件
632 1
|
C# 图形学 数据安全/隐私保护
Unity数据加密☀️ 二、使用Rider将C#代码生成DLL文件
Unity数据加密☀️ 二、使用Rider将C#代码生成DLL文件
下一篇
开通oss服务