狠狠色丁香婷婷综合尤物/久久精品综合一区二区三区/中国有色金属学报/国产日韩欧美在线观看 - 国产一区二区三区四区五区tv

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

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

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

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