当前位置: 移动技术网 > IT编程>开发语言>c# > C#调用7z实现文件的压缩与解压

C#调用7z实现文件的压缩与解压

2020年01月04日  | 移动技术网IT编程  | 我要评论
1.关于7z 首先在这里先介绍一下7z压缩软件,7z是一种主流的 压缩格式,它拥有极高的压缩比。在计算机科学中,7z是一种可以使用多种压缩算法进行数据压缩的档案格式。主要有以下特点: 来源且模块化的组件结构 最高的压缩比 强大的AES-256加密 可更改配置的压缩算法 支持操大文件 支持多线程压缩 ...

1.关于7z

首先在这里先介绍一下7z压缩软件,7z是一种主流的 压缩格式,它拥有极高的压缩比。在计算机科学中,7z是一种可以使用多种压缩算法进行数据压缩的档案格式。主要有以下特点:

  • 来源且模块化的组件结构
  • 最高的压缩比
  • 强大的aes-256加密
  • 可更改配置的压缩算法
  • 支持操大文件
  • 支持多线程压缩
  • 具有多种压缩文件格式
2.解压缩实现代码
实现对文件的解压缩方法是通过cmd命令,调用7z程式通过cmd命令实现对文件进行解压和压缩的操作,具体实现代码如下:
  • 压缩代码
压缩的cmd命令:"7z a -tzip " + zippath + "  " + filepath;
public executionresult compressfile(string filepath, string zippath)//运行dos命令
        {
            executionresult exeres = new executionresult();
            exeres.status = true;
            try
            {
                process process = new process();
                process.startinfo.filename = "cmd.exe";
                string message = "";
                string command1 = "c:";
                string command2 = @"cd\";
                string command3 = @"cd c:\progra~1\7-zip";
                string command4 = "";


                command4 = "7z a -tzip " + zippath + "  " + filepath;

                process.startinfo.useshellexecute = false;
                process.startinfo.redirectstandardinput = true;
                process.startinfo.redirectstandardoutput = true;
                process.startinfo.redirectstandarderror = true;
                process.startinfo.createnowindow = true;
                process.start();
                process.standardinput.writeline(command1);
                process.standardinput.writeline(command2);
                process.standardinput.writeline(command3);
                process.standardinput.writeline(command4);
                process.standardinput.writeline("exit");
                message = process.standardoutput.readtoend(); //要等压缩完成后才可以来抓取这个压缩文件

                process.close();
                if (!message.contains("everything is ok"))
                {
                    exeres.status = false;
                    exeres.message = message;
                }
                else
                {
                    exeres.anything = zippath;
                }
            }
            catch (exception ex)
            {
                exeres.message = ex.message;
            }

            return exeres;
        }

 

  • 解压代码
解压的cmd命令:"7z x -tzip " + zippath + " -o" + filepath + " -y";
public executionresult decompressfile( string zippath, string filepath)//运行dos命令
        {
            executionresult exeres = new executionresult();
            exeres.status = true;
            try
            {
                process process = new process();
                process.startinfo.filename = "cmd.exe";
                string message = "";
                string command1 = "c:";
                string command2 = @"cd\";
                string command3 = @"cd c:\progra~1\7-zip";
                string command4 = "";


                command4 = "7z x -tzip " + zippath + " -o" + filepath + " -y";

                process.startinfo.useshellexecute = false;
                process.startinfo.redirectstandardinput = true;
                process.startinfo.redirectstandardoutput = true;
                process.startinfo.redirectstandarderror = true;
                process.startinfo.createnowindow = true;
                process.start();
                process.standardinput.writeline(command1);
                process.standardinput.writeline(command2);
                process.standardinput.writeline(command3);
                process.standardinput.writeline(command4);
                process.standardinput.writeline("exit");
                //process.waitforexit();
                message = process.standardoutput.readtoend();//要等压缩完成后才可以来抓取这个压缩文件

                process.close();
                if (!message.contains("everything is ok"))
                {
                    exeres.status = false;
                    exeres.message = message;
                }
                else
                {
                    exeres.anything = filepath;
                }
            }
            catch (exception ex)
            {
                exeres.message = ex.message;
            }

            return exeres;
        }

 

 

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网