LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

【C#】关于使用WinRAR进行文件的RAR、ZIP格式解压和压缩操作

admin
2025年2月13日 17:11 本文热度 273

1. 利用 WinRAR 进行解压缩

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Diagnostics;

using Microsoft.Win32;

namespace RarClass

{

    public class unrar

    {

    public void unCompressRAR(string unRarPatch, string rarPatch, string rarName)

        {

            string the_rar;

            RegistryKey the_Reg;

            object the_Obj;

            string the_Info;


            the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");

            the_Obj = the_Reg.GetValue("");

            the_rar = the_Obj.ToString();

            the_Reg.Close();

            //the_rar = the_rar.Substring(1, the_rar.Length – 7);


            if (Directory.Exists(unRarPatch) == false)

            {

                Directory.CreateDirectory(unRarPatch);

            }

            the_Info = "x  -y  " + rarName + " " + unRarPatch ;

            ProcessStartInfo the_StartInfo = new ProcessStartInfo();

            the_StartInfo.FileName = the_rar;

            the_StartInfo.Arguments = the_Info;

            the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

            the_StartInfo.WorkingDirectory = rarPatch;//获取压缩包路径


            Process the_Process = new Process();

            the_Process.StartInfo = the_StartInfo;

            the_Process.Start();

            the_Process.WaitForExit();

            the_Process.Close();

        }

    }

}

2. 利用 WinRAR 进行解压缩

/// <summary>

/// 利用 WinRAR 进行压缩

/// </summary>

/// <param name="path">将要被压缩的文件夹(绝对路径)</param>

/// <param name="rarPath">压缩后的 .rar 的存放目录(绝对路径)</param>

/// <param name="rarName">压缩文件的名称(包括后缀)</param>

/// <returns>true 或 false。压缩成功返回 true,反之,false。</returns>

public bool RAR(string path, string rarPath, string rarName)

{

    bool flag = false;

    string rarexe;       //WinRAR.exe 的完整路径

    RegistryKey regkey;  //注册表键

    Object regvalue;     //键值

    string cmd;          //WinRAR 命令参数

    ProcessStartInfo startinfo;

    Process process;

    try

    {

        regkey = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command");

        regvalue = regkey.GetValue("");  // 键值为 "d:\Program Files\WinRAR\WinRAR.exe" "%1"

        rarexe = regvalue.ToString();    

        regkey.Close();

        rarexe = rarexe.Substring(1, rarexe.Length - 7);  // d:\Program Files\WinRAR\WinRAR.exe


        Directory.CreateDirectory(path);

        //压缩命令,相当于在要压缩的文件夹(path)上点右键->WinRAR->添加到压缩文件->输入压缩文件名(rarName)

        cmd = string.Format("a {0} {1} -r", rarName, path);

        startinfo = new ProcessStartInfo();

        startinfo.FileName = rarexe;

        startinfo.Arguments = cmd;                          //设置命令参数

        startinfo.WindowStyle = ProcessWindowStyle.Hidden;  //隐藏 WinRAR 窗口


        startinfo.WorkingDirectory = rarPath;

        process = new Process();

        process.StartInfo = startinfo;

        process.Start();

        process.WaitForExit(); //无限期等待进程 winrar.exe 退出

        if (process.HasExited)

        {

            flag = true;

        }

        process.Close();

    }

    catch (Exception e)

    {

        throw e;

    }

    return flag;

}


/// <summary>

/// 利用 WinRAR 进行解压缩

/// </summary>

/// <param name="path">文件解压路径(绝对)</param>

/// <param name="rarPath">将要解压缩的 .rar 文件的存放目录(绝对路径)</param>

/// <param name="rarName">将要解压缩的 .rar 文件名(包括后缀)</param>

/// <returns>true 或 false。解压缩成功返回 true,反之,false。</returns>

public bool UnRAR(string path, string rarPath, string rarName)

{

    bool flag = false;

    string rarexe;

    RegistryKey regkey;

    Object regvalue;

    string cmd;

    ProcessStartInfo startinfo;

    Process process;

    try

    {

        regkey = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command");

        regvalue = regkey.GetValue("");

        rarexe = regvalue.ToString();

        regkey.Close();

        rarexe = rarexe.Substring(1, rarexe.Length - 7);//解压缩命令,相当于在要压缩文件(rarName)上点右键->WinRAR->解压到当前文件夹

        cmd = string.Format("x {0} {1} -y",

                            rarName,

                            path);

        startinfo = new ProcessStartInfo();

        startinfo.FileName = rarexe;

        startinfo.Arguments = cmd;

        startinfo.WindowStyle = ProcessWindowStyle.Hidden;= rarPath;

        process = new Process();

        process.StartInfo = startinfo;

        process.Start();

        process.WaitForExit();

        if (process.HasExited)

        {

            flag = true;

        }

        process.Close();

    }

    catch (Exception e)

    {

        throw e;

    }

    return flag;

}


Directory.CreateDirectory(path);

startinfo.WorkingDirectory

昨天又看了下,发现如果路径中有空格(如:D:\Program Files\)的话压缩解压就会出现问题,折磨了我很长时间,最后实在没办法了就在cmd里面试了半天,发现在有空格的路径上加双引号就可以了。在代码里Directory.CreateDirectory(path);后面把 path 和 rarName 都判断一下如果有空格,就加上 path = "\"" + path + "\"";


3. 本次示例主要实现:

  • 1.压缩文件夹及其下文件

  • 2.压缩文件夹下文件

  • 3.压缩文件夹及其下文件为rar 还是 zip

  • 4.解压缩

  • 5.加密压缩及解加密压缩

———–

示例代码如下:

protected void Button1_Click(object sender, EventArgs e)

{

    string strtxtPath = "C:\\freezip\\free.txt";

    string strzipPath = "C:\\freezip\\free.zip";

    System.Diagnostics.Process Process1 = new System.Diagnostics.Process();

    Process1.StartInfo.FileName = "Winrar.exe";

    Process1.StartInfo.CreateNoWindow = true;


    //1、压缩c:\freezip\free.txt(即文件夹及其下文件freezip\free.txt)到c:\freezip\free.rar

    //strzipPath = "C:\\freezip\\free";//默认压缩方式为 .rar

    //Process1.StartInfo.Arguments = " a -r " + strzipPath + " " + strtxtPath;


    //2、压缩c:\freezip\free.txt(即文件夹及其下文件freezip\free.txt)到c:\freezip\free.rar

    //strzipPath = "C:\\freezip\\free";//设置压缩方式为 .zip

    //Process1.StartInfo.Arguments = " a -afzip " + strzipPath + " " + strtxtPath;


    //3、压缩c:\freezip\free.txt(即文件夹及其下文件freezip\free.txt)到c:\freezip\free.zip 直接设定为free.zip

    //Process1.StartInfo.Arguments = " a -r "+strzipPath+" " + strtxtPath ;


    //4、搬迁压缩c:\freezip\free.txt(即文件夹及其下文件freezip\free.txt)到c:\freezip\free.rar 压缩后 原文件将不存在

    //Process1.StartInfo.Arguments = " m " + strzipPath + " " + strtxtPath;


    //5、压缩c:\freezip\下的free.txt(即文件free.txt)到c:\freezip\free.zip 直接设定为free.zip 只有文件 而没有文件夹

    //Process1.StartInfo.Arguments = " a -ep " + strzipPath + " " + strtxtPath;


    //6、解压缩c:\freezip\free.rar到 c:\freezip\

    //strtxtPath = "c:\\freezip\\";

    //Process1.StartInfo.Arguments = " x " + strzipPath + " " + strtxtPath;


    //7、加密压缩c:\freezip\free.txt(即文件夹及其下文件freezip\free.txt)到c:\freezip\free.zip 密码为123456 注意参数间不要空格

    //Process1.StartInfo.Arguments = " a -p123456 " + strzipPath + " " + strtxtPath;


    //8、解压缩加密的c:\freezip\free.rar到 c:\freezip\ 密码为123456 注意参数间不要空格

    //strtxtPath = "c:\\freezip\\";

    //Process1.StartInfo.Arguments = " x -p123456 " + strzipPath + " " + strtxtPath;


   //9、-o+ 覆盖 已经存在的文件; -o- 不覆盖 已经存在的文件

   //strtxtPath = "c:\\freezip\\";

   //Process1.StartInfo.Arguments = " x -o+ " + strzipPath + " " + strtxtPath;


   //10、只从指定的zip中解压出free1.txt到指定路径下压缩包中的其他文件 不予解压

   //strtxtPath = "c:\\freezip\\";

   //Process1.StartInfo.Arguments = " x " + strzipPath + " " +" free1.txt" + " " + strtxtPath;


   //11、通过 -y 对所有询问回应为"是" 以便 即便发生错误 也不弹出WINRAR的窗口 -cl 转换文件名为小写字母

   //strtxtPath = "c:\\freezip\\";

   //Process1.StartInfo.Arguments = " t -y -cl " + strzipPath + " " + " free1.txt";


    Process1.Start();

    if (Process1.HasExited)

    {

       int iExitCode = Process1.ExitCode;

        if (iExitCode == 0)

        {

            Response.Write(iExitCode.ToString() + " 正常完成");

        }

        else

        {

            Response.Write(iExitCode.ToString() + " 有错完成");

        }

    }

}


4. C#调用rar.exe解压一个rar文件到系统的临时目录

//C#调用rar.exe解压一个rar文件到系统的临时目录:

//取得系统临时目录

string sysTempDir = Path.GetTempPath();


//要解压的文件路径,请自行设置

string rarFilePath = @"d:\test.rar";


//确定要解压到的目录,是系统临时文件夹下,与原压缩文件同名的目录里

string unrarDestPath = Path.Combine(sysTempDir, Path.GetFileNameWithoutExtension(rarFilePath));


//组合出需要shell的完整格式

string shellArguments = string.Format("x -o+ \"{0}\" \"{1}\\\"", rarFilePath, unrarDestPath);


//用Process调用

using (Process unrar = new Process())

{

    unrar.StartInfo.FileName = "rar.exe";

    unrar.StartInfo.Arguments = shellArguments;

    //隐藏rar本身的窗口

    unrar.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

    unrar.Start();

    //等待解压完成

    unrar.WaitForExit();

    unrar.Close();

}


//统计解压后的目录和文件数

DirectoryInfo di = new DirectoryInfo(unrarDestPath);

MessageBox.Show(string.Format("解压完成,共解压出:{0}个目录,{1}个文件", di.GetDirectories().Length, di.GetFiles().Length));


该文章在 2025/2/13 17:12:42 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved