c# - 从字节数组读取 .rtf(富文本格式)时过滤错误代码 FILTER_E_ACCESS

标签 c# ifilter

我正在使用这个 http://www.codeproject.com/Articles/31944/Implementing-a-TextReader-to-extract-various-files并且大部分时间都在工作。

我编写此测试是为了检查过滤器是否会按预期从字节数组中读取。

private const string ExpectedText = "This is a test!";
[Test]
public void FilterReader_RtfBytes_TextMatch()
{
    var bytes = File.ReadAllBytes(@"Test Documents\DocTest.rtf");
    var reader = new FilterReader(bytes, ".rtf");
    reader.Init();
    var actualText = reader.ReadToEnd();
    StringAssert.Contains(ExpectedText, actualText);
}

测试失败并返回 ErrorCode : FILTER_E_ACCESS ,当我给它文件名时它工作正常。

new FilterReader(@"Test Documents\DocTest.rtf", ".rtf"); <-- works

我不明白为什么会这样。我查看了代码,似乎是 rtf 过滤器 dll 返回了错误。这更令人费解。

它适用于其他文件类型,例如; .doc、.docx、.pdf

最佳答案

在幕后,使用 iFilter 的具体工作方式由构造函数定义:当您使用构造函数时 FilterReader(byte[] bytes, string extension) 用于从内存加载内容的 IPersistStream,当 FilterReader(string path, string extension) - IPersistFile 用于从文件加载时。

为什么 rtf-ifilter 在与 IPersistStream 一起使用时会返回错误,恐怕我们无法知道,因为源代码未打开

在我的例子中,我将过滤器的具体封装在构造函数中,然后重构代码:

  • 删除所有构造函数
  • 删除public void Init()-method
  • 实现一个自定义构造函数public FilterReader(string fileName, string extension, uint blockSize = 0x2000):

    #region Contracts
    
    Contract.Requires(!string.IsNullOrEmpty(fileName));
    Contract.Requires(!string.IsNullOrEmpty(extension));
    Contract.Requires(blockSize > 1);
    
    #endregion
    
    const string rtfExtension = ".rtf";
    
    FileName = fileName;
    Extension = extension;
    BufferSize = blockSize;
    
    _buffer = new char[ActBufferSize];
    
    // ! Take into account that Rtf-file can be loaded only using IPersistFile.
    var doUseIPersistFile = string.Compare(rtfExtension, extension, StringComparison.InvariantCultureIgnoreCase) == 0;
    
    // Initialize _filter instance.
    try
    {
        if (doUseIPersistFile)
        {
            // Load content using IPersistFile.
            _filter = FilterLoader.LoadIFilterFromIPersistFile(FileName, Extension);
        }
        else
        {
            // Load content using IPersistStream.
            using (var stream = new FileStream(path: fileName, mode: FileMode.Open, access: FileAccess.Read, share: FileShare.Read))
            {
                var buffer = new byte[stream.Length];
                stream.Read(buffer, 0, buffer.Length);
    
                _filter = FilterLoader.LoadIFilterFromStream(buffer, Extension);
            }
        }
    }
    catch (FileNotFoundException)
    {
        throw;
    }
    catch (Exception e)
    {
        throw new AggregateException(message: string.Format("Filter Not Found or Loaded for extension '{0}'.", Extension), innerException: e);
    }
    

关于c# - 从字节数组读取 .rtf(富文本格式)时过滤错误代码 FILTER_E_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10283872/

相关文章:

c++ - 在 x64 系统上从 32 位应用程序使用 IFilter

windows - LibreOffice - 未找到已注册的 IFilter - Windows 7 x64

c# - ChannelFactory.CreateChannel() 是否真的打开了连接?

sharepoint - 如何实现 IFilter 来索引重量级格式?

C# 应用程序在其他计算机上不起作用

c# - 如何使用 Javascript 从下拉列表中获取上一个所选项目和当前所选项目?

c# - 库 IFilter 的问题

azure - Lucene .NET Azure Blob 存储和 IFilter

c# - FluentValidation:使用 ValidationContext 进行验证

c# - 如何使用 OpenXML 使 excel 工作表标题行加粗