WinRAR(WinZip)压缩与解压实现(C#版Window平台)
生活随笔
收集整理的這篇文章主要介紹了
WinRAR(WinZip)压缩与解压实现(C#版Window平台)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
本文的原理是借助Windows平臺安裝的WinRAR(WinZip)實現(xiàn)C#程序的調(diào)用(注:WinRAR壓縮解壓WinZip同樣適用)。
?
先來看WinRAR(WinZip)自身的支持調(diào)用命令:
壓縮命令: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")
?
參數(shù)說明
| 參數(shù) | 含義 |
| a | 添加文件到壓縮包 |
| x | 以完整路徑從壓縮包解開壓縮 |
?
?
?
?
?
WinZip(WinRAR)調(diào)用通用類
using System;using System.Collections.Generic;
using System.Text;
//--------------using
using System.Diagnostics;
using Microsoft.Win32;
using System.IO;
/// <summary>
/// Name:Stone
/// DateTime: 2011/12/31 16:39:26
/// Description:WinRAR壓縮
/// </summary>
public class WinRARCSharp
{
// WinRAR安裝注冊表key
private const string WinRAR_KEY = @"WinRAR.ZIP\shell\open\command";
/// <summary>
/// 利用 WinRAR 進(jìn)行壓縮
/// </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(WinRAR_KEY);
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(); //無限期等待進(jìn)程 winrar.exe 退出
if (process.HasExited)
{
flag = true;
}
process.Close();
}
catch (Exception e)
{
throw e;
}
return flag;
}
/// <summary>
/// 利用 WinRAR 進(jìn)行解壓縮
/// </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(WinRAR_KEY);
regvalue = regkey.GetValue("");
rarexe = regvalue.ToString();
regkey.Close();
rarexe = rarexe.Substring(1, rarexe.Length - 7);
Directory.CreateDirectory(path);
//解壓縮命令,相當(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;
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;
}
}
調(diào)用方法
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:\\MM\\GG”目錄下)
?
7z壓縮通用類:
using System;using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Diagnostics;
using System.IO;
/// <summary>
/// Name:Stone
/// DateTime: 2012/1/4 16:26:08
/// Description:7Z解壓管理類
/// </summary>
public class _7zRAR
{
// 7z.exe 安裝地址
private const string _7zEXE = @"D:\Program Files (x86)\7-Zip\7z.exe";
/// <summary>
/// 利用 7zExE 進(jìn)行壓縮
/// </summary>
/// <param name="_7zPath">將要被壓縮的文件夾(物理路徑)</param>
/// <param name="filePath">壓縮后的的存放目錄(物理路徑)</param>
/// <returns>true 或 false。壓縮成功返回 true,反之,false。</returns>
public static bool Un7zRAR(string _7zPath, string filePath)
{
bool flag = false;
string cmd;
ProcessStartInfo startinfo;
Process process;
try
{
cmd = String.Format(@"x {0} -o{1} -y",
_7zPath, filePath);
startinfo = new ProcessStartInfo();
startinfo.FileName = _7zEXE;
startinfo.Arguments = cmd;
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
process = new Process();
process.StartInfo = startinfo;
process.Start();
process.WaitForExit();
if (process.HasExited)
{
flag = true;
}
process.Close();
}
catch (Exception e)
{
throw e;
}
return flag;
}
}
?
?
=============《完》===================
轉(zhuǎn)載請寫明出處:http://www.cnblogs.com/stone_w/archive/2012/01/04/2312294.html
?
總結(jié)
以上是生活随笔為你收集整理的WinRAR(WinZip)压缩与解压实现(C#版Window平台)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 任务调度的使用crontab
- 下一篇: 天气预报HTML代码