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; }
原文:https://www.cnblogs.com/wml-it/p/12148778.html