首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SevenZipSharp进行快速压缩

使用SevenZipSharp进行快速压缩
EN

Stack Overflow用户
提问于 2017-04-18 17:32:19
回答 1查看 1.2K关注 0票数 1

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

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

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

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

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

代码语言:javascript
复制
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.743Bytes。

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

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

EN

回答 1

Stack Overflow用户

发布于 2017-04-18 19:14:57

将您的代码生成的归档文件的大小与通过上下文菜单生成的归档文件的大小进行比较。代码中耗时较长的过程是否会产生较小的文件。另外,请确认您获得的压缩比,因为不清楚原始文件的可压缩性。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43468496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档