使用SevenZipSharp进行快速压缩

时间:2017-04-18 09:32:19

标签: c# 7zip sevenzipsharp

我正在寻找一种快速创建包含大量小文件的目录的.zip存档(例如25.4 MB,8个目录和4505个文件,但可能更大)。

当我使用标准7zip安装(通过上下文菜单)时,压缩需要1到2秒。

当我使用SevenZipSharp库中的SevenZipCompressor在C#应用程序中执行相同操作时,它需要更长时间(> 5秒)。现在我想知道7zip使用的默认参数是什么,如何在我的代码中设置它们以达到相同的速度?

对于我的应用程序,压缩级别不如速度重要。

这是我的代码(我尝试了不同的压缩级别和模式,但没有显着差异):

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);
}

原始目录的大小为26.678.577字节。

使用c#代码创建的压缩.zip为25.786.743 Bytes。

使用7zip安装创建的压缩.zip为25.771.350 Bytes。

我也尝试使用BeginCompressDirectory代替CompressDirectory,但这根本不起作用。它会立即返回,不会触发任何事件,只会创建一个空档案。

1 个答案:

答案 0 :(得分:0)

将使用您的代码生成的存档文件的大小与通过上下文菜单生成的文件大小进行比较。代码中的进程需要更长时间才能生成更小的文件。此外,请确认您获得的压缩率,因为不清楚原始文件的可压缩程度。