1
votes

I am looking for a fast way to create a .zip archive of a directory that contains a lot of small files (e.g. 25.4 MB, 8 directories and 4505 files, but could be larger).

When I use the standard 7zip Installation (via context menu) the compression takes 1 to 2 seconds.

When I use the SevenZipCompressor from the SevenZipSharp library to do the same in a C# application, it takes way longer (> 5 seconds). Now I am wondering what are the default parameters used by 7zip and how can I set them in my code to achieve the same speed?

For my application, the compression level is not as important as the speed.

Here is my code (I tried different compression levels and modes, but with no significant differences):

public Compressor()
{
  var zipFile = @"pathTo7ZipDll\7z.dll";
  if (File.Exists(zipFile))
  {
    SevenZipBase.SetLibraryPath(zipFile);
  }
  else
  {
    throw new ApplicationException("seven zip dll file not found!");
  }

  Zipper = new SevenZipCompressor
  {
    ArchiveFormat = OutArchiveFormat.Zip,
    DirectoryStructure = true,
    PreserveDirectoryRoot = true,
    CompressionLevel = CompressionLevel.Fast,
    CompressionMethod = CompressionMethod.Deflate
  };


  Zipper.FileCompressionStarted += (s, e) =>
  {
    if (IsCancellationRequested)
    {
      e.Cancel = true;
    }
  };

  Zipper.Compressing += (s, e) =>
  {
    if (IsCancellationRequested)
    {
      e.Cancel = true;
      return;
    }

    if (e.PercentDone == 100)
    {
      OnFinished();
    }
    else
    {
      Console.WriteLine($"Progress received: {e.PercentDone}.");
    }
  };

  Zipper.CompressionFinished += (s, e) =>
  {
    OnFinished();
  };
}

private void OnFinished()
{
  IsProcessing = false;
  IsCancellationRequested = false;
}

public void StartCompression()
{
  IsProcessing = true;
  Zipper.CompressDirectory(InputDir, OutputFilePath);
}

The original directory has a size of 26.678.577 Bytes.

The compressed .zip created with the c# code is 25.786.743 Bytes.

The compressed .zip created with the 7zip installation is 25.771.350 Bytes.

I also tried using BeginCompressDirectory instead of CompressDirectory, but that does not work at all. It returns immediately with no events being fired and only creates an empty archive.

1
Try Zipper.CustomParameters.Add("mt", "on"); to tell it to use multiple threads. - Alex K.
Unfortunately this does not make any difference. - tabina
The compression with my c# code takes 6167 ms. When I add the multi-threading option, it is 6289 ms. - tabina
Did you eventually resolve this problem? - Snympi

1 Answers

0
votes

Compare the size of the archive file produced with you code to the one produced via the context menu. Does the process in your code which takes longer, produce a smaller file. Also, please confirm the compression ratio which you are getting because it is unclear how compressible the original files are.