Unzip a file in c# using 7z.exe
我正试图从WinForm应用程序中解压缩一个文件。我正在使用此代码:
1 2 3 4 5 6 7 | string dezarhiverPath = @AppDomain.CurrentDomain.BaseDirectory +"\\7z.exe"; ProcessStartInfo pro = new ProcessStartInfo(); pro.WindowStyle = ProcessWindowStyle.Hidden; pro.FileName = dezarhiverPath; pro.Arguments = @" e c:\TEST.ZIP"; Process x = Process.Start(pro); x.WaitForExit(); |
代码不返回错误,但不返回任何内容。我还尝试了命令,命令来自命令:
1 | K:\>"C:\Test\7z.exe" e"c:\TEST.ZIP" |
但在命令中,我收到了以下错误消息:
1 | 7-Zip cannot find the code that works with archives. |
有人能帮我从C解压一些文件吗?
谢谢!
为什么您要在外部使用7z.exe应用程序?这是一种非常笨拙的方法。相反,您可以使用许多库中的一个。
如果这是一个新的应用程序,而您的目标是.NET 4.5,那么新的
或者,
也可以使用DotNetZip,它是MS PL许可的。
这也许能帮到你。
1 2 3 4 5 | //You must create an empty folder to remove. string tempDirectoryPath = @"C:\Users\HOPE\Desktop\Test Folder\zipfolder"; string zipFilePath = @"C:\Users\HOPE\Desktop\7za920.zip"; Directory.CreateDirectory(tempDirectoryPath); ZipFile.ExtractToDirectory(zipFilePath, tempDirectoryPath); |
嘿,使用下面的代码,您的系统中必须有7zip应用程序。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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 } } |
创造:
1 2 3 4 5 6 7 8 9 10 11 | 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(); } |
试试这个
1 2 3 4 5 6 7 8 9 | string fileZip = @"c:\example esult.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(); |
您可以使用Sevenzipsharp库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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); } } } } |
请参阅以下代码:
1 2 3 4 5 6 7 8 9 10 | using System.IO.Compression; string startPath = @"c:\example\start"; string zipPath = @"c:\example esult.zip"; string extractPath = @"c:\example\extract"; ZipFile.CreateFromDirectory(startPath, zipPath); ZipFile.ExtractToDirectory(zipPath, extractPath); |
参考链接:
http://social.msdn.microsoft.com/forums/en-us/csharpgeneral/thread/849c4969-24b1-4650-88a5-5169727e527f/