C#在Windows上調(diào)用7-zip實(shí)現(xiàn)壓縮文件
更新時(shí)間:2023年10月27日 14:20:45 作者:先生沉默先
這篇文章主要為大家詳細(xì)介紹了C#如何在Windows上調(diào)用7-zip實(shí)現(xiàn)壓縮文件,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以學(xué)習(xí)一下
使用C#在Windows上調(diào)用7-zip壓縮文件
- 可以設(shè)置輸出文件的路徑也可以留空,留空則會(huì)在壓縮文件創(chuàng)建一個(gè)同名的.壓縮包
- 可以設(shè)置壓縮包的密碼
- 可以設(shè)置壓縮包的加密方式(ASE-256),可以使用LZMA但是加密碼會(huì)報(bào)錯(cuò)
- 可以設(shè)置壓縮包的格式(zip),可以使用7z但是加密碼會(huì)報(bào)錯(cuò)
- 添加了密碼最大長(zhǎng)度的限制(98個(gè)字符,7zip限制的)
- 在7-ZIP的圖形界面可以選擇7z格式壓縮可以輸入中文的密碼
示例代碼
using System;
using System.Diagnostics;
namespace 文件的壓縮
{
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine("你好,接下來(lái)開(kāi)始?jí)嚎s文件");
ZipsHelper.CompressedInformation compressedInformation = new ZipsHelper.CompressedInformation(
@"E:\壓縮文件測(cè)試\壓縮文件_Orgion\V_1696602827.txt",
"",
"",
ZipsHelper.CompressedFileType.Zip,
ZipsHelper.CompressedPackageEncryptionMode.AES256);
//壓縮 E:\壓縮文件測(cè)試\壓縮文件_Orgion\V1696602827.txt
//到 E:\壓縮文件測(cè)試\壓縮文件_Orgion\V1696602827.zip
ZipsHelper.DoCompressedFile(compressedInformation);
Console.ReadKey();
}
}
/// <summary>
/// zip文件壓縮
/// </summary>
public class ZipsHelper
{
/// <summary>
/// 壓縮文件
/// </summary>
public static void DoCompressedFile(CompressedInformation compressedInformation)
{
// 設(shè)置7-Zip可執(zhí)行文件的路徑,根據(jù)你的安裝路徑進(jìn)行修改
string sevenZipExePath = @"C:\Program Files\7-Zip\7z.exe";
if (!System.IO.File.Exists(sevenZipExePath))
{
Console.WriteLine($"未能找到7z.exe ,請(qǐng)檢查路徑,當(dāng)前路徑是:{sevenZipExePath}");
return;
}
if (compressedInformation.Password.Length > 98)
{
Console.WriteLine($"壓縮取消,密碼長(zhǎng)度過(guò)長(zhǎng),最大長(zhǎng)度是98,當(dāng)前長(zhǎng)度是:{compressedInformation.Password.Length}。");
return;
}
string encryptionMethod;//壓縮包的加密方式
if (compressedInformation.CompressedPackageEncryptionMode == CompressedPackageEncryptionMode.AES256)
{
encryptionMethod = "-mem=AES256";
}
//else if (compressedInformation.CompressedPackageEncryptionMode == CompressedPackageEncryptionMode.LZMA)
//{
//encryptionMethod = "-mhe=on -m0=BCJ2 -m1=LZMA2 -m2=LZMA2 -m3=LZMA2 -mb0:1 -mb0s1:2 -mb0s2:3";
//}
else
{
encryptionMethod = "-mem=AES256";
}
string format;//設(shè)置壓縮包的格式
if (compressedInformation.CompressedFileType == CompressedFileType.Zip)
{
compressedInformation.CompressedFilePath += ".zip";//添加壓縮包的文件后綴
format = "zip";
}
else
{
format = "7z";
}
string arguments;//壓縮的參數(shù)
//構(gòu)建7-Zip命令行參數(shù)
if (compressedInformation.Password == "")//當(dāng)選擇了壓縮的加密方式但是密碼為空的時(shí)候不能壓縮
{
arguments = $"a -t{format} \"{compressedInformation.CompressedFilePath}\" \"{compressedInformation.FilePathToCompress}\"";
}
else
{
arguments = $"a -t{format} \"{compressedInformation.CompressedFilePath}\" \"{compressedInformation.FilePathToCompress}\" {encryptionMethod} -p{compressedInformation.Password}";
}
Console.WriteLine(arguments);
// 創(chuàng)建一個(gè)新的進(jìn)程來(lái)運(yùn)行7-Zip
Process process = new Process();
process.StartInfo.FileName = sevenZipExePath;
process.StartInfo.Arguments = arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
// 啟動(dòng)7-Zip進(jìn)程并等待其完成
process.Start();
process.WaitForExit();
// 處理輸出結(jié)果
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
if (string.IsNullOrEmpty(error))
{
Console.WriteLine("文件壓縮成功!");
}
else
{
Console.WriteLine("文件壓縮失敗,錯(cuò)誤信息:" + error);
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(output);
}
/// <summary>
/// 壓縮包類(lèi)型
/// </summary>
public enum CompressedFileType
{
Zip = 1,
// _7Z = 2
}
/// <summary>
/// 壓縮包加密格式
/// </summary>
public enum CompressedPackageEncryptionMode
{
AES256,
// LZMA,
}
public class CompressedInformation
{
/// <summary>
/// 壓縮文件路徑
/// </summary>
private string filePathToCompress;
/// <summary>
/// 輸出文件路徑
/// </summary>
private string compressedFilePath;
/// <summary>
/// 密碼
/// </summary>
private string password;
/// <summary>
/// 壓縮包類(lèi)型
/// </summary>
private CompressedFileType compressedFileType;
/// <summary>
/// 壓縮包加密格式
/// </summary>
private CompressedPackageEncryptionMode compressedPackageEncryptionMode;
public string FilePathToCompress { get => filePathToCompress; set => filePathToCompress = value; }
public string CompressedFilePath { get => compressedFilePath; set => compressedFilePath = value; }
public string Password { get => password; set => password = value; }
public CompressedFileType CompressedFileType { get => compressedFileType; set => compressedFileType = value; }
public CompressedPackageEncryptionMode CompressedPackageEncryptionMode { get => compressedPackageEncryptionMode; set => compressedPackageEncryptionMode = value; }
/// <summary>
/// 壓縮命令參數(shù)
/// </summary>
/// <param name="filePathToCompress">壓縮文件路徑</param>
/// <param name="compressedFilePath">壓縮包輸出路徑</param>
/// <param name="password">密碼</param>
/// <param name="compressedFileType">壓縮包格式</param>
/// <param name="compressedPackageEncryptionMode">壓縮包加密方式</param>
public CompressedInformation(string filePathToCompress,
string compressedFilePath = "",
string password = "",
CompressedFileType compressedFileType = CompressedFileType.Zip,
CompressedPackageEncryptionMode compressedPackageEncryptionMode = CompressedPackageEncryptionMode.AES256)
{
this.FilePathToCompress = filePathToCompress;
this.CompressedFilePath = compressedFilePath;
this.Password = password;
this.CompressedFileType = compressedFileType;
this.CompressedPackageEncryptionMode = compressedPackageEncryptionMode;
if (compressedFilePath == "")
{
GetFileNameAndExtension(filePathToCompress, out compressedFilePath);
this.CompressedFilePath = compressedFilePath;
}
}
public static void GetFileNameAndExtension(string filePath, out string pathWithoutExtension)
{
pathWithoutExtension = System.IO.Path.ChangeExtension(filePath, null); // 去除文件后綴
}
}
}
}到此這篇關(guān)于C#在Windows上調(diào)用7-zip實(shí)現(xiàn)壓縮文件的文章就介紹到這了,更多相關(guān)C#壓縮文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#利用iTextSharp組件給PDF文檔添加圖片/文字水印
這篇文章主要給大家介紹了關(guān)于如何C#利用iTextSharp組件給PDF文檔添加圖片/文字水印的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
使用C#實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)堆的代碼
這篇文章主要介紹了使用C#實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)堆,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
C#中ListView控件實(shí)現(xiàn)窗體代碼
這篇文章主要介紹了C#中ListView控件實(shí)現(xiàn)窗體的核心代碼,非常不錯(cuò),具有參考借鑒價(jià)值,對(duì)c#listview相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-08-08
c#后臺(tái)輸出javascript語(yǔ)句示例程序
一個(gè)很不錯(cuò)的b/s前臺(tái)輸出彈出對(duì)話框、后臺(tái)寫(xiě)javascript語(yǔ)句、后臺(tái)直接關(guān)閉web頁(yè)面及一個(gè)集成了常用驗(yàn)證的通用類(lèi),十分的方便。代碼如下2013-12-12
C#的FileSystemWatcher用法實(shí)例詳解
這篇文章主要介紹了C#的FileSystemWatcher用法,以實(shí)例形似詳細(xì)分析了FileSystemWatcher控件主要功能,并總結(jié)了FileSystemWatcher控件使用的技巧,需要的朋友可以參考下2014-11-11

