IFilter 错误代码FILTER_E_ACCESS从字节数组读取.rtf(里克文本格式)时
本文关键字:rtf 格式 读取 文本 数组 FILTER 错误代码 ACCESS 字节数 字节 IFilter | 更新日期: 2023-09-27 18:31:57
我正在使用这个 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);
}
测试失败并显示错误代码: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()
方法 -
实现一个自定义构造函数
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); }