C#通過cmd調(diào)用7z軟件實(shí)現(xiàn)壓縮和解壓文件
更新時(shí)間:2022年04月14日 14:39:44 作者:農(nóng)碼一生
這篇文章介紹了C#通過cmd調(diào)用7z軟件實(shí)現(xiàn)壓縮和解壓文件的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
壓縮文件:
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, "解壓完成" }; } else { resObj = new object[] { 1, "解壓出錯(cuò)!" }; } } catch (Exception ex) { resObj = new object[] { 1, ex.Message }; } return resObj; }
到此這篇關(guān)于C#通過cmd調(diào)用7z軟件實(shí)現(xiàn)壓縮和解壓文件的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- C#壓縮或解壓rar、zip文件方法實(shí)例
- C#調(diào)用7z實(shí)現(xiàn)文件的壓縮與解壓
- 詳解C#壓縮、解壓文件夾/文件(帶密碼)
- C#使用GZipStream實(shí)現(xiàn)文件的壓縮與解壓
- c# 文件壓縮zip或?qū)ip文件解壓的方法
- c#打包文件解壓縮的實(shí)例
- C#實(shí)現(xiàn)壓縮和解壓縮的方法示例【Gzip和Zip方式】
- C#實(shí)現(xiàn)文件壓縮與解壓的方法示例【ZIP格式】
- ASP.NET 文件壓縮解壓類(C#)
- C#使用WinRar命令進(jìn)行壓縮和解壓縮操作的實(shí)現(xiàn)方法
- C#中ZipHelper 壓縮和解壓幫助類
相關(guān)文章
C# CancellationToken和CancellationTokenSource的用法詳解
做了.net core之后,發(fā)現(xiàn)CancellationToken用的越來越平凡了。這也難怪,原來.net framework使用異步的不是很多,而.net core首推異步編程,到處可以看到Task的影子,而CancellationToken正好是異步Task的一個(gè)控制器,所以花點(diǎn)時(shí)間做個(gè)筆記2021-06-06C#中的Task.WhenAll和Task.WhenAny方法介紹
這篇文章介紹了C#中的Task.WhenAll和Task.WhenAny方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04WPF實(shí)現(xiàn)多運(yùn)算符表達(dá)式計(jì)算器
這篇文章主要為大家詳細(xì)介紹了WPF實(shí)現(xiàn)多運(yùn)算符表達(dá)式計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11