C#調用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;
        }

 

  • 解壓代碼
解壓的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;
        }
相關文章
相關標籤/搜索