Miral
Miral

Reputation: 395

Performance issues at extracting 7z file to a directory c#

I'm using SharpCompress library for extracting .7z files but it takes about 35 mins to extract 60mb .7z file. Is this normal or am I doing something wrong in terms of performance? .7z file is compressed in high compress mode and LZMA type.

 using (var archive2 = ArchiveFactory.Open(source))
 {
     foreach (var entry in archive2.Entries)
     {
         if (!entry.IsDirectory)
         {
             entry.WriteToDirectory(destination, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
             }
         }
      }
  }

Upvotes: 0

Views: 1628

Answers (1)

Aximili
Aximili

Reputation: 29484

This is an old post, but I just had the same problem.

This line is the problem

foreach (var entry in archive2.Entries)

The problem is described here (ie. If there are 100 files, it decompresses the 1st file 100 times, 2nd file 99 times, and so on)

The solution is to use reader (forward-only). See the API.
But the sample code there doesn't support 7z.

For 7z you can use archive.ExtractAllEntries(), eg.

using (var archive = ArchiveFactory.Open(movedZipFile))
{
    var reader = archive.ExtractAllEntries();
    while (reader.MoveToNextEntry())
    {
        if (!reader.Entry.IsDirectory)
            reader.WriteEntryToDirectory(extractDir, new ExtractionOptions() { ExtractFullPath = false, Overwrite = true });
    }
}

It will be much faster.

Upvotes: 2

Related Questions