【问题标题】:Fast compression with SevenZipSharp使用 SevenZipSharp 进行快速压缩
【发布时间】:2017-04-18 22:53:27
【问题描述】:

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

当我使用标准 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 字节。

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

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

【问题讨论】:

  • 尝试Zipper.CustomParameters.Add("mt", "on"); 告诉它使用多个线程。
  • 不幸的是,这没有任何区别。
  • 用我的 c# 代码压缩需要 6167 毫秒。当我添加多线程选项时,它是 6289 毫秒。
  • 您最终解决了这个问题吗?

标签: c# 7zip sevenzipsharp


【解决方案1】:

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

【讨论】:

  • 我添加了文件大小。
  • 文件大小相似,压缩率低,所以很遗憾那里没有更多信息。您是否尝试过其他 CompressionMethod 选项,例如 Zipper.CompressionMethod = CompressionMethod.Lzma
  • 是的,我尝试了不同的方法。他们都需要相当长的时间。对于 Lzma,我什至没有收到任何 Compressing 事件。
  • 也许最后一件事是,您是否在附加调试器的情况下进行时间测试。您是否尝试过在不启动调试器的情况下以发布模式运行测试?
  • 发布和调试模式没有显着差异。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-22
  • 2015-08-25
  • 1970-01-01
  • 1970-01-01
  • 2017-05-14
相关资源
最近更新 更多