1

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.

tabina
  • 1,095
  • 1
  • 14
  • 37

1 Answers1

0

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.

Snympi
  • 859
  • 13
  • 18
  • I added the file sizes. – tabina Apr 18 '17 at 11:21
  • File sizes are similar and compression ratio is low, so unfortunately not much more information there. Have you tried with other CompressionMethod options such as `Zipper.CompressionMethod = CompressionMethod.Lzma` – Snympi Apr 18 '17 at 12:27
  • Yes, I tried different methods. They all take rather long. For the Lzma I don't even get any Compressing events. – tabina Apr 18 '17 at 14:56
  • Last thing to try perhaps, are you doing the time tests with the debugger attached. Have you tried running the test in Release mode without launching the debugger? – Snympi Apr 19 '17 at 13:20
  • 1
    Release and Debug mode make no significant difference. – tabina Jul 28 '17 at 07:06