溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在C#項(xiàng)目中利用7z實(shí)現(xiàn)一個(gè)文件壓縮功能

發(fā)布時(shí)間:2020-12-07 14:48:42 來源:億速云 閱讀:606 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)怎么在C#項(xiàng)目中利用7z實(shí)現(xiàn)一個(gè)文件壓縮功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

1.關(guān)于7z

7z是一種主流的 壓縮格式,它擁有極高的壓縮比。在計(jì)算機(jī)科學(xué)中,7z是一種可以使用多種壓縮算法進(jìn)行數(shù)據(jù)壓縮的檔案格式。主要有以下特點(diǎn):

  • 來源且模塊化的組件結(jié)構(gòu)
  • 最高的壓縮比
  • 強(qiáng)大的AES-256加密
  • 可更改配置的壓縮算法
  • 支持操大文件
  • 支持多線程壓縮
  • 具有多種壓縮文件格式
     

2.解壓縮實(shí)現(xiàn)代碼

實(shí)現(xiàn)對(duì)文件的解壓縮方法是通過cmd命令,調(diào)用7z程式通過cmd命令實(shí)現(xiàn)對(duì)文件進(jìn)行解壓和壓縮的操作,具體實(shí)現(xiàn)代碼如下:

  • 壓縮代碼

壓縮的cmd命令:"7Z a -tzip " + zipPath + "  " + filePath;

public ExecutionResult CompressFile(string filePath, string zipPath)//運(yùn)行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(); //要等壓縮完成后才可以來抓取這個(gè)壓縮文件

        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)//運(yùn)行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();//要等壓縮完成后才可以來抓取這個(gè)壓縮文件

        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;
    }

看完上述內(nèi)容,你們對(duì)怎么在C#項(xiàng)目中利用7z實(shí)現(xiàn)一個(gè)文件壓縮功能有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

7z
AI