【发布时间】:2020-05-27 09:03:39
【问题描述】:
下面是我正在为 LZMA (7zip) SDK 找到的 here 构建的一个类的一小段摘录。在大多数情况下,一切似乎都正常,但我正在尝试实施一些进度报告来跟踪压缩过程的进度。在encoder.Code(inStream, outStream, -1, -1, null); 下面的行中,当我需要使用某种形式的ICodeProgress 回调时,我使用null。
我只想创建某种类型的委托回调来跟踪我在班级中创建的线程中的进度。
类摘录:
public class CompressorThreads : CompressorClassEvents
{
public static class SevenZipCoderProperties
{
public static readonly CoderPropID[] PropertyNames =
{
CoderPropID.DictionarySize,
CoderPropID.PosStateBits,
CoderPropID.LitContextBits,
CoderPropID.LitPosBits,
CoderPropID.Algorithm,
CoderPropID.NumFastBytes,
CoderPropID.MatchFinder,
CoderPropID.EndMarker,
};
public static readonly object[] PropertyValues =
{
(Int32)LzmaDictionarySize.VerySmall,
(Int32)(2),
(Int32)(3),
(Int32)(0),
(Int32)(2),
(Int32)LzmaSpeed.Fastest,
"bt4",
true,
};
}
private void Compress(string filePath, string compressedFilePath)
{
//Get the bytes of the file
byte[] fileInBytes = File.ReadAllBytes(filePath);
//Setup the encoder
SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
encoder.SetCoderProperties(SevenZipCoderProperties.PropertyNames, SevenZipCoderProperties.PropertyValues);
using (MemoryStream outStream = new MemoryStream())
{
encoder.WriteCoderProperties(outStream);
//Get the file bytes into a memory then compress them
using (MemoryStream inStream = new MemoryStream(fileInBytes))
{
long uncompressedFilesize = inStream.Length;
for (int i = 0; i < 8; i++)
outStream.WriteByte((Byte)(uncompressedFilesize >> (8 * i)));
encoder.Code(inStream, outStream, -1, -1, null);
}
//Write the compressed file
compressedFileSize = outStream.Length;
using (FileStream file = new FileStream(compressedFilePath, FileMode.Create, FileAccess.Write))
{
outStream.WriteTo(file);
}
}
}
}
【问题讨论】:
-
显示包含 Compress 函数的类的声明。
-
@JimRhodes 完成。我编辑了问题,还删除了我在问题中写的所有内联代表垃圾。我编辑了您的答案,以反映您要求的新信息。