简体   繁体   English

C#中的7-zip提取

[英]7-zip extraction in C#

I have a folder containing a million zip files which I need to extract. 我有一个文件夹,其中包含需要提取的一百万个zip文件。 Two ways I have tried: 我尝试过两种方法:

Way 1: Use the 7-zip exe file and System.Diagnostic Process as follows 方法1:使用7-zip exe文件和System.Diagnostic Process,如下所示

 ProcessStartInfo p = new ProcessStartInfo();
 p.FileName = "7za.exe" 
 p.Arguments = "e " + sourceName; // sourceName is the name of the zip file
 p.WindowStyle = ProcessWindowStyle.Hidden;
 Process x = Process.Start(p);
 x.WaitForExit();

Way 2: Use the 7z.dll and SevenZipExtractor as follows 方法2:使用7z.dll和SevenZipExtractor,如下所示

 SevenZipExtractor.SetLibraryPath(@"C:\Program Files\7-Zip\7z.dll");
 SevenZipExtractor zipFile = new SevenZipExtractor("inv1_0.zip");
 zipFile.ExtractArchive(@"c:\projects\extractor\extractor\bin\debug\inv1_0.dat");

Two things here: 这里有两件事:

a) Way 1 works perfectly fine. a)方法1完全正常。 Way 2 doesn't work. 方法2不起作用。 Give the error "SevenZip.SevenZipArchiveException: Invalid archive: open/read error!". 给出错误“ SevenZip.SevenZipArchiveException:无效的存档:打开/读取错误!”。 Can someone suggest why this could be happening when way 1 is giving the correct extracted file? 有人可以建议为什么在方法1提供正确的提取文件时会发生这种情况吗?

b) Since there are a million zip files, I am worried about using Way 1 : as in the Process. b)由于存在一百万个zip文件,因此我担心像过程中那样使用方式1: How expensive is to loop through a million zip files and use process for each one of them. 循环遍历一百万个zip文件并使用每个文件的过程要花费多少。 I would really like to use Way 2 but for some reason it gives the exception. 我真的很想使用Way 2,但是由于某种原因它给出了例外。

You can't avoid the cost of unzipping 1,000,000 files, but you can choose the way it'll cost you - processor usage, or time. 您无法避免解压缩1,000,000个文件的成本,但是您可以选择要花费的方式-处理器使用率或时间。 If you don't want to lock your main thread, let a secondary thread handle the file processing for you. 如果您不想锁定主线程,请让辅助线程为您处理文件处理。

Options: 选项:

But that doesn't seem to be your issue at all. 但这似乎根本不是您的问题。 It seems to me that SevenZipExtractor is behaving in a different way than the standalone .exe. 在我看来,SevenZipExtractor的行为方式不同于独立的.exe。 I would obviously stick to the first (working) option until I figured out what's wrong with the 2nd approach. 显然,我会坚持使用第一个(有效的)选项,直到我发现第二种方法出了什么问题。

You may also want to pay a visit to sevenzipsharp's CodePlex discussion group . 您可能还想访问sevenzipsharp的CodePlex讨论组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM