我正在寻找一种快速的方式来创建一个包含大量小文件(例如25.4 MB,8目录和4505文件,但可能更大)的目录的.zip存档。SevenZipSharp快速压缩
当我使用标准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”);'告诉它使用多个线程。 –
不幸的是,这并没有什么区别。 – tabina
我的C#代码的压缩需要6167毫秒。当我添加多线程选项时,它是6289毫秒。 – tabina