C#調(diào)用7z實(shí)現(xiàn)文件的壓縮與解壓
1.關(guān)于7z
首先在這里先介紹一下7z壓縮軟件,7z是一種主流的 壓縮格式,它擁有極高的壓縮比。在計(jì)算機(jī)科學(xué)中,7z是一種可以使用多種壓縮算法進(jìn)行數(shù)據(jù)壓縮的檔案格式。主要有以下特點(diǎn):
- 來源且模塊化的組件結(jié)構(gòu)
- 最高的壓縮比
- 強(qiáng)大的AES-256加密
- 可更改配置的壓縮算法
- 支持操大文件
- 支持多線程壓縮
- 具有多種壓縮文件格式
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;
}
以上就是C#調(diào)用7z實(shí)現(xiàn)文件的壓縮與解壓的詳細(xì)內(nèi)容,更多關(guān)于c# 文件壓縮與解壓的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#在Windows上調(diào)用7-zip實(shí)現(xiàn)壓縮文件
這篇文章主要為大家詳細(xì)介紹了C#如何在Windows上調(diào)用7-zip實(shí)現(xiàn)壓縮文件,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以學(xué)習(xí)一下2023-10-10
自定義實(shí)現(xiàn)Json字符串向C#對(duì)象轉(zhuǎn)變的方法
自定義實(shí)現(xiàn)Json字符串向C#對(duì)象轉(zhuǎn)變的方法,需要的朋友可以參考一下2013-03-03
c# OpenCvSharp實(shí)現(xiàn)常見檢測(cè)(斑點(diǎn)檢測(cè),輪廓檢測(cè),邊緣檢測(cè))
這篇文章主要為大家詳細(xì)介紹了c#如何使用OpenCvSharp實(shí)現(xiàn)常見檢測(cè)(斑點(diǎn)檢測(cè),輪廓檢測(cè),邊緣檢測(cè)),文中的示例代碼講解詳細(xì),需要的小伙伴可以參考下2023-12-12
C#實(shí)現(xiàn)廣度優(yōu)先搜索的實(shí)例代碼
廣度優(yōu)先搜索算法(BFS)是圖或樹搜索的重要算法,學(xué)習(xí)BFS能提高理解圖結(jié)構(gòu)的能力,對(duì)解決復(fù)雜圖問題有幫助,實(shí)際應(yīng)用包括網(wǎng)絡(luò)爬蟲、社交網(wǎng)絡(luò)分析、迷宮求解等,感興趣的可以了解一下2024-10-10
C# 使用鼠標(biāo)點(diǎn)擊對(duì)Chart控件實(shí)現(xiàn)數(shù)據(jù)提示效果
這篇文章主要介紹了C# 使用鼠標(biāo)點(diǎn)擊對(duì)Chart控件實(shí)現(xiàn)數(shù)據(jù)提示效果,文章給予上一篇的詳細(xì)內(nèi)容做延伸介紹,需要的小伙伴可任意參考一下2022-08-08
C#實(shí)現(xiàn)讓ListBox適應(yīng)最大Item寬度的方法
這篇文章主要介紹了C#實(shí)現(xiàn)讓ListBox適應(yīng)最大Item寬度的方法,涉及ListBox控件的操作技巧,需要的朋友可以參考下2015-05-05

