我正在寻找一种快速的方法来创建包含大量小文件(例如 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
,但这根本不起作用。它会立即返回,不会触发任何事件,并且只会创建一个空存档。