C#实现Zip文件解压

首先需要安装7-Zip。7-Zip是一款高压缩比的压缩软件,不仅支持独有的7z文件格式,而且还支持各种其它压缩文件格式,其中包括 ZIP、RAR、CAB、GZIP、BZIP2和TAR等格式。此软件压缩的压缩比要比普通ZIP文件高30-50%。

方式一:

/// <summary>
/// 解压Zip文件
/// </summary>
public static void Unzip()
{
    string zipFileName = @"E:\MyZip\Report.zip";   //需要被解压的Zip文件
    string unZipPath = @"E:\MyZip\";               //解压后文件存放目录
    string pwd = "123";                            //解压密码

    Process process = new Process();
    process.StartInfo.FileName = @"C:\Program Files\7-Zip\7z.exe";   //7-Zip工具的运行程序
    process.StartInfo.Arguments = String.Format("e \"{0}\" -p{1} -o\"{2}\"", zipFileName, pwd, unZipPath);
    process.Start();
}

方式二:

创建ProcessHelper.cs类。

public class ProcessHelper
{
    public static string[] ExecCommand(string commands)
    {
        //msg[0]执行结果;msg[1]错误结果
        string[] msg = new string[2];
        Process proc = new Process();
        try
        {
            proc.StartInfo.FileName = "cmd.exe";
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardInput = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.CreateNoWindow = true;
            proc.Start();

            proc.StandardInput.WriteLine(commands);
            proc.StandardInput.WriteLine("exit");

            //执行结果
            msg[0] = proc.StandardOutput.ReadToEnd();
            proc.StandardOutput.Close();

            //出错结果
            msg[1] = proc.StandardError.ReadToEnd();
            proc.StandardError.Close();

            //超时等待
            int maxWaitCount = 10;
            while (proc.HasExited == false && maxWaitCount > 0)
            {
                proc.WaitForExit(1000);
                maxWaitCount--;
            }
            if (maxWaitCount == 0)
            {
                msg[1] = "操作执行超时";
                proc.Kill();
            }
            return msg;
        }
        catch (Exception ex)
        {
            msg[1] = "进程创建失败:";
            msg[1] += ex.Message.ToString();
            msg[1] += ex.StackTrace.ToString();
        }
        finally
        {
            proc.Close();
            proc.Dispose();
        }
        return msg;
    }
}

调用方法:

/// <summary>
/// 解压Zip文件
/// </summary>
public static void Unzip()
{
    string zipExe = @"C:\Program Files\7-Zip\7z.exe";   //7-Zip工具的运行程序
    string zipFileName = @"E:\MyZip\Report.zip";        //需要被解压的Zip文件
    string unZipPath = @"E:\MyZip\";                    //解压后文件存放目录
    string pwd = "123";                                 //解压密码

    //执行解压命令
    string cmd = String.Format("\"{0}\" e \"{1}\" -p{2} -o\"{3}\"", zipExe, zipFileName, pwd, unZipPath);
    ProcessHelper.ExecCommand(cmd);
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

pan_junbiao

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

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

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

打赏作者

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

抵扣说明:

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

余额充值