一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net在windows平台实现WinRAR压缩与解压的实现方法

时间:2022-06-25 08:23:30 编辑:袖梨 来源:一聚教程网

先来看WinRAR自身的支持调用命令:

压缩命令:a {0} {1} -r 【{0}:压缩后文件名|{1}:待压缩的文件物理路径】

ex:"a 你妹.rar f:MM -r" (含义为将f盘下MM的文件夹压缩为"你妹.rar"文件)

解压命令:x {0} {1} -y 【{0}:待解压文件名称|{1}:待解压文件物理路径】

ex:"x 幺妹.rar f:幺妹 -y"(待压缩文件物理路径:"f:幺妹幺妹.rar")

参数说明

参数

含义a

添加文件到压缩包x

以完整路径从压缩包解开压缩

c#调用通用类

 代码如下 复制代码

using System;
using System.Collections.Generic;
using System.Text;

//--------------using
using System.Diagnostics;
using Microsoft.Win32;
using System.IO;

///


/// Name:Stone
/// DateTime: 2011/12/31 16:39:26
/// Description:WinRAR压缩
///

public class WinRARCSharp
{
    // WinRAR安装注册表key
    private const string WinRAR_KEY = @"WinRAR.ZIPshellopencommand";

    ///


    /// 利用 WinRAR 进行压缩
    ///

    /// 将要被压缩的文件夹(绝对路径)
    /// 压缩后的 .rar 的存放目录(绝对路径)
    /// 压缩文件的名称(包括后缀)
    /// true 或 false。压缩成功返回 true,反之,false。
    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(WinRAR_KEY);
            regvalue = regkey.GetValue("");  // 键值为 "d:Program FilesWinRARWinRAR.exe" "%1"
            rarexe = regvalue.ToString();
            regkey.Close();
            rarexe = rarexe.Substring(1, rarexe.Length - 7);  // d:Program FilesWinRARWinRAR.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;
    }
    ///


    /// 利用 WinRAR 进行解压缩
    ///

    /// 文件解压路径(绝对)
    /// 将要解压缩的 .rar 文件的存放目录(绝对路径)
    /// 将要解压缩的 .rar 文件名(包括后缀)
    /// true 或 false。解压缩成功返回 true,反之,false。
    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(WinRAR_KEY);
            regvalue = regkey.GetValue("");
            rarexe = regvalue.ToString();
            regkey.Close();
            rarexe = rarexe.Substring(1, rarexe.Length - 7);

            Directory.CreateDirectory(path);
            //解压缩命令,相当于在要压缩文件(rarName)上点右键->WinRAR->解压到当前文件夹
            cmd = string.Format("x {0} {1} -y",
                                rarName,
                                path);
            startinfo = new ProcessStartInfo();
            startinfo.FileName = rarexe;
            startinfo.Arguments = cmd;
            startinfo.WindowStyle = ProcessWindowStyle.Hidden;

            startinfo.WorkingDirectory = 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;
    }
}

调用方法

 代码如下 复制代码

WinRARCSharp win = new WinRARCSharp();

win.RAR("F:aaa", "f:", "a.rar"); // 压缩(将“f:aaa”目录文件压缩到“f:a.rar”)

win.UnRAR("f:呦M.zip", "f:MM", "GG"); // 解压(将“f:呦M.zip”解压到“f:MMGG”目录下)

热门栏目