使用7zip压缩文件的示例C#.net代码

我在C:\ Program文件的机器上安装了7-zip 4.65。 我想在C#代码中使用它来压缩文件。 文件名将由用户动态提供。 任何人都可以提供一个如何在C#代码中使用7zip的示例代码吗?

而不是二进制版本,您需要源代码。

这可以作为LZMA SDK获得。

在那里你会找到一个文件夹CS ,其中包含7zip文件算法的C#实现。

上面给出了很多答案,但我在下面提到了使用7zip压缩或解压缩文件的代码

你的系统必须有7zip应用程序。

  public void ExtractFile(string source, string destination) { // If the directory doesn't exist, create it. if (!Directory.Exists(destination)) Directory.CreateDirectory(destination); string zPath = @"C:\Program Files\7-Zip\7zG.exe"; // change the path and give yours 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) { //DO logic here } } 

创建zip文件

 public void CreateZip() { string sourceName = @"d:\a\example.txt"; string targetName = @"d:\a\123.zip"; ProcessStartInfo p = new ProcessStartInfo(); p.FileName = @"C:\Program Files\7-Zip\7zG.exe"; p.Arguments = "a -tgzip \"" + targetName + "\" \"" + sourceName + "\" -mx=9"; p.WindowStyle = ProcessWindowStyle.Hidden; Process x = Process.Start(p); x.WaitForExit(); } 

您是否尝试过7zip的C#接口: http : //www.codeproject.com/KB/DLL/cs_interface_7zip.aspx

[编辑]看起来已经回答了这个问题: C#的免费压缩库支持7zip(LZMA)

更多图书馆:

http://www.eggheadcafe.com/tutorials/aspnet/064b41e4-60bc-4d35-9136-368603bcc27a/7zip-lzma-inmemory-com.aspx

http://sevenzipsharp.codeplex.com/

http://www.7-zip.org/sdk.html – 从官方网站上最好使用这个

或者您可以使用J#zip库(包含在.Net Framework中)示例: http : //weblogs.asp.net/jgalloway/archive/2007/10/25/creating-zip-archives-in-net -without-AN-外部库样sharpziplib.aspx

我想如果你想使用你在c:\ program files中安装的那个,你可以使用System.Diagnostics.Process来运行命令行应用程序 – http://msdn.microsoft.com/en-us/library/ system.diagnostics.process.aspx

传递参数也很容易。 这里有很多例子 – http://www.c-sharpcorner.com/UploadFile/DipalChoksi/ShellCommandsInCS12032005042031AM/ShellCommandsInCS.aspx