C#調(diào)用7z實現(xiàn)文件的壓縮與解壓
1.關(guān)于7z
首先在這里先介紹一下7z壓縮軟件,7z是一種主流的 壓縮格式,它擁有極高的壓縮比。在計算機科學(xué)中,7z是一種可以使用多種壓縮算法進行數(shù)據(jù)壓縮的檔案格式。主要有以下特點:
- 來源且模塊化的組件結(jié)構(gòu)
- 最高的壓縮比
- 強大的AES-256加密
- 可更改配置的壓縮算法
- 支持操大文件
- 支持多線程壓縮
- 具有多種壓縮文件格式
2.解壓縮實現(xiàn)代碼
實現(xiàn)對文件的解壓縮方法是通過cmd命令,調(diào)用7z程式通過cmd命令實現(xiàn)對文件進行解壓和壓縮的操作,具體實現(xiàn)代碼如下:
- 壓縮代碼
壓縮的cmd命令:"7Z a -tzip " + zipPath + " " + filePath;
public ExecutionResult CompressFile(string filePath, string zipPath)//運行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(); //要等壓縮完成后才可以來抓取這個壓縮文件 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)//運行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();//要等壓縮完成后才可以來抓取這個壓縮文件 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實現(xiàn)文件的壓縮與解壓的詳細內(nèi)容,更多關(guān)于c# 文件壓縮與解壓的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#在Windows上調(diào)用7-zip實現(xiàn)壓縮文件
這篇文章主要為大家詳細介紹了C#如何在Windows上調(diào)用7-zip實現(xiàn)壓縮文件,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以學(xué)習(xí)一下2023-10-10自定義實現(xiàn)Json字符串向C#對象轉(zhuǎn)變的方法
自定義實現(xiàn)Json字符串向C#對象轉(zhuǎn)變的方法,需要的朋友可以參考一下2013-03-03c# OpenCvSharp實現(xiàn)常見檢測(斑點檢測,輪廓檢測,邊緣檢測)
這篇文章主要為大家詳細介紹了c#如何使用OpenCvSharp實現(xiàn)常見檢測(斑點檢測,輪廓檢測,邊緣檢測),文中的示例代碼講解詳細,需要的小伙伴可以參考下2023-12-12C# 使用鼠標點擊對Chart控件實現(xiàn)數(shù)據(jù)提示效果
這篇文章主要介紹了C# 使用鼠標點擊對Chart控件實現(xiàn)數(shù)據(jù)提示效果,文章給予上一篇的詳細內(nèi)容做延伸介紹,需要的小伙伴可任意參考一下2022-08-08C#實現(xiàn)讓ListBox適應(yīng)最大Item寬度的方法
這篇文章主要介紹了C#實現(xiàn)讓ListBox適應(yīng)最大Item寬度的方法,涉及ListBox控件的操作技巧,需要的朋友可以參考下2015-05-05