C#通過cmd調(diào)用7z軟件實現(xiàn)壓縮和解壓文件
更新時間:2022年04月14日 14:39:44 作者:農(nóng)碼一生
這篇文章介紹了C#通過cmd調(diào)用7z軟件實現(xiàn)壓縮和解壓文件的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
壓縮文件:
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, "解壓出錯!" };
}
}
catch (Exception ex)
{
resObj = new object[] { 1, ex.Message };
}
return resObj;
}到此這篇關(guān)于C#通過cmd調(diào)用7z軟件實現(xiàn)壓縮和解壓文件的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實現(xiàn)定時任務(wù)Task Scheduler的示例代碼
這篇文章主要為大家詳細介紹了C#實現(xiàn)定時任務(wù)Task Scheduler的相關(guān)知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02
C#實現(xiàn)String字符串轉(zhuǎn)化為SQL語句中的In后接的參數(shù)詳解
在本篇文章中小編給大家分享的是一篇關(guān)于C#實現(xiàn)String字符串轉(zhuǎn)化為SQL語句中的In后接的實例內(nèi)容和代碼,需要的朋友們參考下。2020-01-01
C# 連接SQL數(shù)據(jù)庫的方法及常用連接字符串
這篇文章主要介紹了C# 連接SQL數(shù)據(jù)庫的方法及常用連接字符串,有需要的朋友可以參考一下2014-01-01
C# Csv實現(xiàn)基本的讀寫和轉(zhuǎn)換DataTable
本文主要介紹了C# Csv實現(xiàn)基本的讀寫和轉(zhuǎn)換DataTable,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02

