惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threat Research - Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
P
Proofpoint News Feed
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
A
About on SuperTechFans
T
Tenable Blog
M
MIT News - Artificial intelligence
IT之家
IT之家
I
Intezer
D
DataBreaches.Net
爱范儿
爱范儿
T
Threatpost
C
CERT Recently Published Vulnerability Notes
云风的 BLOG
云风的 BLOG
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
K
Kaspersky official blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
Cyberwarzone
Cyberwarzone
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
Spread Privacy
Spread Privacy
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AWS News Blog
AWS News Blog
博客园 - 聂微东
C
Check Point Blog
S
Securelist
有赞技术团队
有赞技术团队
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
D
Docker
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tailwind CSS Blog
L
Lohrmann on Cybersecurity
G
Google Developers Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog

博客园 - 飞舞的蒲公英

开发式新手引导设计思想 flash系统奔溃的主要原因 c#真正判断文件类型 winform文件拖入 c#winform选择文件,文件夹,打开指定目录方法 C#图片无损压缩 as3.0 动态文本属性大全 卡​马​克​卷​轴​算​法​研​究​_​地​图​双​缓​冲 春卷活动心得 As3 常用日期工具 As3 计算两个日期之间的天数差 解决Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题 C#图片压缩算法 C#图片处理之: 另存为压缩质量可自己控制的JPEG - 飞舞的蒲公英 C# :实现水印与图片合成,并利用Graphics 压缩图像质量 , (委托实现listBox的动态添加提示) - 飞舞的蒲公英 C#放缩、截取、合并图片并生成高质量新图的类 - 飞舞的蒲公英 手机游戏模拟器汇总 用于开发 - 飞舞的蒲公英 SQL SERVER 2008 无法启动T-SQL调试的解决方法 - 飞舞的蒲公英 WinAPI 操作串口 - 飞舞的蒲公英
WinRAR(WinZip)压缩与解压实现(C#版Window平台)
飞舞的蒲公英 · 2015-03-20 · via 博客园 - 飞舞的蒲公英

本文的原理是借助Windows平台安装的WinRAR(WinZip)实现C#程序的调用(注:WinRAR压缩解压WinZip同样适用)。

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

压缩命令: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

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

WinZip(WinRAR)调用通用类

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 进行压缩
    /// </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(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);
            //压缩命令,相当于在要压缩的文件夹(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(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:\\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 进行压缩
    /// </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;
    }
}