C#文件操作集合七(文件压缩相关)

前言

        文件操作是开发过程中经常遇到的,如何创建文件、处理文件、保存文件以及读取文件这些工作是每一位开发都必须经历的。

这里围绕C#文件操作内容,我做了一次系统的梳理,主要包括以下几个模块:

1、文件内容操作:文件读写相关

2、文件检测

3、文件及目录创建和删除

4、文件移动

5、文件加密、解密

6、文件上传、下载

7、文件压缩相关

8、文件操作权限相关


一、小节摘要

本小节实例主要讲述C#实现使用系统WinRar进行压缩和解压缩的方法,详细内容如下:

1、压缩方法

2、解压缩方法

二、详细内容

1、压缩方法

/// <summary>
/// 压缩
/// </summary>
/// <param name="path">要压缩文件路径</param>
/// <param name="rarPath">要压缩的文件名</param>
/// <param name="rarName">压缩的文件路径</param>
public static void EnZip(string path, string rarName, string rarPath)
{
    //   bool flag = false;
    try
    {
        the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\Shell\Open\Command");
        the_Obj = the_Reg.GetValue("");
        the_rar = the_Obj.ToString();
        the_Reg.Close();
        the_rar = the_rar.Substring(1, the_rar.Length - 7);
        the_Info = " a   " + rarPath + "  " + path;
        the_StartInfo = new ProcessStartInfo();
        the_StartInfo.FileName = the_rar;
        the_StartInfo.Arguments = the_Info;
        the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        //打包文件存放目录
        the_StartInfo.WorkingDirectory = rarName;
        the_Process = new Process();
        the_Process.StartInfo = the_StartInfo;
        the_Process.Start();
        the_Process.WaitForExit();
        the_Process.Close();
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

压缩参数命令参数如下:
1
压缩即文件夹及其下文件
//the_Info = " a    " + rarName + "  " + path + "  -r";              
2
压缩即文件夹及其下文件 设置压缩方式为 .zip
//the_Info = " a -afzip  " + rarName + "  " + path;  
3
压缩文件夹及其下文件 直接设定为free.zip
//the_Info = " a -r  " + rarName + "  " + path;
4
搬迁压缩即文件夹及其下文件原文件将不存在
//the_Info = " m  " + rarName + "  " + path;
5
压缩即文件  直接设定为free.zip 只有文件 而没有文件夹
//the_Info = " a -ep  " + rarName + "  " + path;
6
加密压缩即文件夹及其下文件 密码为123456 注意参数间不要空格
//the_Info = " a -p123456  " + rarName + "  " + path;

2、解压缩方法 

/// <summary>
/// 解压缩
/// </summary>
/// <param name="zipname">要解压的文件名</param>
/// <param name="zippath">要解压的文件路径</param>
public static void DeZip(string zipname, string zippath)
{
    try
    {
        the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRar.exe\Shell\Open\Command");
        the_Obj = the_Reg.GetValue("");
        the_rar = the_Obj.ToString();
        the_Reg.Close();
        the_rar = the_rar.Substring(1, the_rar.Length - 7);
        the_Info = " X " + zipname + " " + zippath;
        the_StartInfo = new ProcessStartInfo();
        the_StartInfo.FileName = the_rar;
        the_StartInfo.Arguments = the_Info;
        the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        the_Process = new Process();
        the_Process.StartInfo = the_StartInfo;
        the_Process.Start();
        the_Process.WaitForExit();
  
        the_Process.Close();
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

3、变量相关 

#region 私有变量
static String the_rar; //WinRAR.exe 的完整路径 
static RegistryKey the_Reg; //注册表键 
static Object the_Obj; //键值 
static String the_Info;  //cmd命令值
static ProcessStartInfo the_StartInfo;
static Process the_Process;
#endregion

//64位系统
//此时会提示:未将对象引用设置为对象的实例 
//解决办法:修改注册表,添加如下项:
//HKEY_CLASSES_ROOT\Applications\WinRAR.exe\Shell\Open\Command 
//值为:"C:\Program Files (x86)\WinRAR\WinRAR.exe" "%1"

#region 调用外部RAR解压缩
private static string rarRegPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe";

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MarcoPro

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值