当前位置:   article > 正文

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

cmd调用7z

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;
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

解压代码
解压的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;
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

c#教程

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/320592
推荐阅读
相关标签
  

闽ICP备14008679号