温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在C#项目中利用7z实现一个文件压缩功能

发布时间:2020-12-07 14:48:42 来源:亿速云 阅读:588 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关怎么在C#项目中利用7z实现一个文件压缩功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

1.关于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;
    }

看完上述内容,你们对怎么在C#项目中利用7z实现一个文件压缩功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

7z
AI