我如何为LZMA(7zip)SDK编码器创建回调函数。Code()函数?

问题描述 投票:0回答:1

下面是我正在为here找到的LZMA(7zip)SDK构建的一个类的摘录。在大多数情况下,一切似乎都可以正常运行,但是我正在尝试实现一些进度报告以跟踪压缩过程的进度。在encoder.Code(inStream, outStream, -1, -1, null);下面的行中,当我需要使用某种形式的null回调时,我会使用ICodeProgress。我不确定如何使它正常工作,但是我想做一些类似于以下内容的内联...

var progress = new Action<long, long>(processedInSize, processedOutSize =>
{
    System.Diagnostics.Debug.WriteLine("processedInSize:" + processedInSize + "  processedOutSize:" + processedOutSize);
});

或者也许...

ICodeProgress progress = new Action<long, long>((processedInSize, processedOutSize) =>
{
    System.Diagnostics.Debug.WriteLine("processedInSize:" + processedInSize + "  processedOutSize:" + processedOutSize);
});

我并不是说任何一个都远非正确,但我只想创建某种类型的委托回调来跟踪我在类中创建的线程中的进度。

课堂摘录:

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);
        }
    }
}
c# delegates 7zip lzma
1个回答
0
投票

ICodeProgress是一个接口,因此您需要在某个地方实现它。您可以在您的类中实现ICodeProgress接口,并传递this而不是null

例如,如果您的类名为MyEncoder,则将执行以下操作:

class MyEncoder : ICodeProgress
{
    void SetProgress(Int64 inSize, Int64 outSize)
    {
        System.Diagnostics.Debug.WriteLine("processedInSize:" + inSize + "  processedOutSize:" + outSize);
    }
    private void Compress(string filePath, string compressedFilePath)
    {
        . . .
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.