1.關於7z算法
首先在這裏先介紹一下7z壓縮軟件,7z是一種主流的 壓縮格式,它擁有極高的壓縮比。在計算機科學中,7z是一種可使用多種壓縮算法進行數據壓縮的檔案格式。主要有如下特色:多線程
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; }
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; }