午夜视频在线观看区二区-午夜视频在线观看视频-午夜视频在线观看视频在线观看-午夜视频在线观看完整高清在线-午夜视频在线观看网站-午夜视频在线观看亚洲天堂

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

【C#】關(guān)于使用WinRAR進行文件的RAR、ZIP格式解壓和壓縮操作

admin
2025年2月13日 17:11 本文熱度 410

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 命令參數(shù)

    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);

        //壓縮命令,相當(dāng)于在要壓縮的文件夾(path)上點右鍵->WinRAR->添加到壓縮文件->輸入壓縮文件名(rarName)

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

        startinfo = new ProcessStartInfo();

        startinfo.FileName = rarexe;

        startinfo.Arguments = cmd;                          //設(shè)置命令參數(shù)

        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);//解壓縮命令,相當(dāng)于在要壓縮文件(rarName)上點右鍵->WinRAR->解壓到當(dāng)前文件夾

        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

昨天又看了下,發(fā)現(xiàn)如果路徑中有空格(如:D:\Program Files\)的話壓縮解壓就會出現(xiàn)問題,折磨了我很長時間,最后實在沒辦法了就在cmd里面試了半天,發(fā)現(xiàn)在有空格的路徑上加雙引號就可以了。在代碼里Directory.CreateDirectory(path);后面把 path 和 rarName 都判斷一下如果有空格,就加上 path = "\"" + path + "\"";


3. 本次示例主要實現(xiàn):

  • 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";//設(shè)置壓縮方式為 .zip

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


    //3、壓縮c:\freezip\free.txt(即文件夾及其下文件freezip\free.txt)到c:\freezip\free.zip 直接設(shè)定為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 直接設(shè)定為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 注意參數(shù)間不要空格

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


    //8、解壓縮加密的c:\freezip\free.rar到 c:\freezip\ 密碼為123456 注意參數(shù)間不要空格

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

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


   //9、-o+ 覆蓋 已經(jīng)存在的文件; -o- 不覆蓋 已經(jīng)存在的文件

   //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 對所有詢問回應(yīng)為"是" 以便 即便發(fā)生錯誤 也不彈出WINRAR的窗口 -cl 轉(zhuǎn)換文件名為小寫字母

   //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#調(diào)用rar.exe解壓一個rar文件到系統(tǒng)的臨時目錄

//C#調(diào)用rar.exe解壓一個rar文件到系統(tǒng)的臨時目錄:

//取得系統(tǒng)臨時目錄

string sysTempDir = Path.GetTempPath();


//要解壓的文件路徑,請自行設(shè)置

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


//確定要解壓到的目錄,是系統(tǒng)臨時文件夾下,與原壓縮文件同名的目錄里

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


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

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


//用Process調(diào)用

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();

}


//統(tǒng)計解壓后的目錄和文件數(shù)

DirectoryInfo di = new DirectoryInfo(unrarDestPath);

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


該文章在 2025/2/13 17:12:42 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調(diào)度、堆場、車隊、財務(wù)費用、相關(guān)報表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務(wù)都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

主站蜘蛛池模板: 国产精品午夜福利不卡 | 国产精品高清一区二区三区 | 国产高清无码视频专区 | 国产丝袜美腿一区二区三区 | 2025韩国三级午夜理论 | 韩国无遮挡三级伦在线观看 | 高清av一级大片 | 2025国语电影免费在线观看 | 成人午夜精品久久久久久久小说 | 国产一区二区二区 | 91午夜成人影院在线观看 | a级国产乱理伦片 | av无码中文专区 | 国产欧美日韩综合在线成 | 99久久精品一区二区毛片 | av无码免费在线一区二区三区 | 韩国黄色漫画在线看在线阅读 | 爆乳无码系 | 国产成人精品日本亚洲专区 | 国产精品对白交换绿帽视频 | 国产成人香港三级录像视频 | 国产无码三级片精品网址 | 成人无码网www在线观看 | 国产在线一区二区在线视频 | 精品无码一区在线观看动漫 | 国产精品亚洲片牛牛 | 国产成人啪精品 | a级毛片无码免费真 | 国产一区在线免费观看 | 2025最新无码免费 | 国产成人综合在线观看网站 | 99精品国产高清一区二区麻豆 | 国产成人av福利在线播放 | 国产精品萌白酱在线观看 | 国产无套视频在线观看 | 韩国精品一区二区三区久久 | 国产熟女亚洲精品麻豆 | 国内第一永久免费福利视频 | 国产成人无码a区在线观看软件 | www亚洲精品少妇裸乳一区 | 成人免费一区二区三区视频软件 |