【问题标题】:How do I create a callback function for the LZMA (7zip) SDK encoder.Code() function?如何为 LZMA (7zip) SDK 编码器.Code() 函数创建回调函数?
【发布时间】: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 完成。我编辑了问题,还删除了我在问题中写的所有内联代表垃圾。我编辑了您的答案,以反映您要求的新信息。

标签: c# delegates 7zip lzma


【解决方案1】:

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

例如,你会做这样的事情:

class CompressorThreads : CompressorClassEvents, ICodeProgress
{
    void ICodeProgress.SetProgress(long inSize, long outSize)
    {
        System.Diagnostics.Debug.WriteLine("processedInSize:" + inSize + "  processedOutSize:" + outSize);
    }
    private void Compress(string filePath, string compressedFilePath)
    {
        . . .
        encoder.Code(inStream, outStream, -1, -1, this);
        . . .
    }
}

【讨论】:

  • 啊!好的!我会试一试。没有意识到这就是您使用接口的方式。谢谢!
  • 看起来它成功了,现在可以编译了。后续问题,我可以在 SetProgress 回调函数上使用access modifier 吗?我似乎没有尝试使用任何东西,例如:public、internal、protected 等。谢谢!
  • 是的,我认为应该是public。
  • 它说不。 :)The modifier 'public' is not valid for this item。似乎只喜欢没有修饰符。 :(
  • 你需要省略修饰符。这里有解释:stackoverflow.com/a/2669140/798346
猜你喜欢
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2015-10-06
  • 2017-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多