通过cmd调用7z软件实现压缩和解压缩文件
通过cmd调用7z软件实现压缩和解压缩文件
压缩文件:
public object CompressZipFile(string sourceFile, string destinationFile) { object resObj; Process process = new Process(); string tempDestinationFile = ""; try { if (process == null) { process = new Process(); } tempDestinationFile = destinationFile.ToLower().EndsWith(".zip") ? destinationFile : destinationFile + ".zip"; process.StartInfo.FileName = "cmd.exe"; string command1 = @"cd c:\progra~1\7-zip"; 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(@"c:"); process.StandardInput.WriteLine(@"cd\"); process.StandardInput.WriteLine(command1); process.StandardInput.WriteLine(@"7Z a -tzip " + destinationFile + " " + sourceFile); process.StandardInput.WriteLine("exit"); string output = process.StandardOutput.ReadToEnd(); if (output.ToUpper().IndexOf("EVERYTHING IS OK") > -1) { resObj = new object[] { 0, "Compress file[" + destinationFile + "] OK" }; } else { resObj = new object[] { 1, "Compress File[" + destinationFile + "] Error!" }; } } catch (Exception ex) { resObj = new object[] { 1, "Compress File[" + destinationFile + "] exception:" + ex.Message }; } return resObj; }
解压文件:
public object decompressFile(string zipFilepath, string FileDir) { object resObj; try { string batfiledata = @"c:\progra~1\7-zip\7z.exe x " + zipFilepath + " -o" + FileDir + " -y"; Process process = new Process(); process.StartInfo.FileName = "cmd.exe"; process.StartInfo.Arguments = "/c " + batfiledata; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.CreateNoWindow = true; process.Start(); string output = process.StandardOutput.ReadToEnd(); if (output.ToUpper().IndexOf("EVERYTHING IS OK") > -1) { resObj = new object[] { 0, "解壓縮OK" }; } else { resObj = new object[] { 1, "解壓縮出錯!" }; } } catch (Exception ex) { resObj = new object[] { 1, ex.Message }; } return resObj; }
本文来自博客园,作者:码农阿亮,转载请注明原文链接:https://www.cnblogs.com/wml-it/p/14729429.html
技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!
开源库地址,欢迎点亮:
GitHub:https://github.com/ITMingliang
Gitee: https://gitee.com/mingliang_it
GitLab: https://gitlab.com/ITMingliang
建群声明: 本着技术在于分享,方便大家交流学习的初心,特此建立【编程内功修炼交流群】,为大家答疑解惑。热烈欢迎各位爱交流学习的程序员进群,也希望进群的大佬能不吝分享自己遇到的技术问题和学习心得!进群方式:扫码关注公众号,后台回复【进群】。