| 1.關(guān)于7z 首先在這里先介紹一下7z壓縮軟件,7z是一種主流的 壓縮格式,它擁有極高的壓縮比。在計(jì)算機(jī)科學(xué)中,7z是一種可以使用多種壓縮算法進(jìn)行數(shù)據(jù)壓縮的檔案格式。主要有以下特點(diǎn): 
 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; } 
 | 
|  |