如何在 C# 中使用 7Zip 压缩文件夹? [英] How would I compress a folder with 7Zip in C#?
本文介绍了如何在 C# 中使用 7Zip 压缩文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将一个文件夹压缩成一个扩展名为 .7z 的文件,使用 7zip.
I want to compress a folder into a file with the .7z extension, with 7zip.
我想知道我会怎么做,因为我不确定(这就是我问的原因.)
I would like to know how I would do this, because I'm not sure (which is why I'd asking.)
这是在 C# 中.
页面链接或示例代码会很有帮助.
Links to pages or sample code would be helpful.
推荐答案
使用 7zip 压缩或解压文件的代码
code to zip or unzip file using 7zip
此代码用于压缩文件夹
public void CreateZipFolder(string sourceName, string targetName)
{
// this code use for zip a folder
sourceName = @"d:Data Files"; // folder to be zip
targetName = @"d:Data Files.zip"; // zip name you can change
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = @"D:7-Zip7z.exe";
p.Arguments = "a -t7z "" + targetName + "" "" + sourceName + "" -mx=9";
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
x.WaitForExit();
}
此代码用于压缩文件
public void CreateZip(string sourceName, string targetName)
{
// file name to be zip , you must provide file name with extension
sourceName = @"d:ipmsg.log";
// targeted file , you can change file name
targetName = @"d:ipmsg.zip";
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = @"D:7-Zip7z.exe";
p.Arguments = "a -tgzip "" + targetName + "" "" + sourceName + "" -mx=9";
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
x.WaitForExit();
}
此代码用于解压
public void ExtractFile(string source, string destination)
{
// If the directory doesn't exist, create it.
if (!Directory.Exists(destination))
Directory.CreateDirectory(destination);
string zPath = @"D:7-Zip7zG.exe";
try
{
ProcessStartInfo pro = new ProcessStartInfo();
pro.WindowStyle = ProcessWindowStyle.Hidden;
pro.FileName = zPath;
pro.Arguments = "x "" + source + "" -o" + destination;
Process x = Process.Start(pro);
x.WaitForExit();
}
catch (System.Exception Ex) { }
}
这篇关于如何在 C# 中使用 7Zip 压缩文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文