使用7z.exe在c#中解压缩文件

我正在尝试从winform应用程序解压缩文件。 我正在使用此代码:

string dezarhiverPath = @AppDomain.CurrentDomain.BaseDirectory + "\\7z.exe"; ProcessStartInfo pro = new ProcessStartInfo(); pro.WindowStyle = ProcessWindowStyle.Hidden; pro.FileName = dezarhiverPath; pro.Arguments = @" ec:\TEST.ZIP"; Process x = Process.Start(pro); x.WaitForExit(); 

代码不会返回错误但不会返回任何错误。 我也从cmd尝试过这个命令:

 K:\>"C:\Test\7z.exe" e "c:\TEST.ZIP" 

但在cmd中,我收到此错误消息:

 7-Zip cannot find the code that works with archives. 

有人可以帮我从c#解压缩一些文件吗?

谢谢!

你为什么要在外部尝试使用7z.exe应用程序呢? 这是一种非常有用的方式。 而是使用众多库中的一个。

如果这是一个新的应用程序,并且您的目标是.NET 4.5,则新的System.IO.Compression命名空间具有ZipFile类。

或者, SharpZipLib是一个用于.NET中文件压缩的​​GPL库。 有在线样本 。

另外还有获得Ms-PL许可的DotNetZip 。

参考以下代码:

 using System.IO.Compression; string startPath = @"c:\example\start"; string zipPath = @"c:\example\result.zip"; string extractPath = @"c:\example\extract"; ZipFile.CreateFromDirectory(startPath, zipPath); ZipFile.ExtractToDirectory(zipPath, extractPath); 

Referance链接:

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/849c4969-24b1-4650-88a5-5169727e527f/

您可以使用SevenZipSharp库

 using (var input = File.OpenRead(lstFiles[0])) { using (var ds = new SevenZipExtractor(input)) { //ds.ExtractionFinished += DsOnExtractionFinished; var mem = new MemoryStream(); ds.ExtractFile(0, mem); using (var sr = new StreamReader(mem)) { var iCount = 0; String line; mem.Position = 0; while ((line = sr.ReadLine()) != null && iCount < 100) { iCount++; LstOutput.Items.Add(line); } } } } 

试试这个

  string fileZip = @"c:\example\result.zip"; string fileZipPathExtactx= @"c:\example\"; ProcessStartInfo p = new ProcessStartInfo(); p.WindowStyle = ProcessWindowStyle.Hidden; p.FileName = dezarhiverPath ; p.Arguments = "x \"" + fileZip + "\" -o" + fileZipPathExtact; Process x = Process.Start(p); x.WaitForExit(); 

嘿,请使用下面的代码,您的系统中必须有7zip应用程序。

  public void ExtractFile(string source, string 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 } } 

创造 :

 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(); }