打开 OLM 文件
Contents
[
Hide
]
一切从一个 OlmStorage 对象。它可以通过文件路径、任何可读取的流创建,或者对于可能损坏的文件,通过接受错误回调的构造函数创建。本文介绍了这三种方式。
以下示例使用以下头文件和命名空间:
#include <Storage/Olm/OlmStorage.h>
#include <Storage/Olm/OlmFolder.h>
#include <Exceptions/TraversalExceptionsCallback.h>
#include <Exceptions/TraversalAsposeException.h>
#include <system/io/file.h>
#include <system/collections/list_ext.h>
#include <system/convert.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Storage::Olm;
using namespace Aspose::Email::Exceptions;
通过路径打开文件
使用构造函数
将文件名传递给 OlmStorage 构造函数会打开存档并立即读取其文件夹树,因此, FolderHierarchy 属性已就绪。调用 释放 完成后释放文件句柄。
String fileName = u"SampleOLM.olm";
SharedPtr<OlmStorage> olm = MakeObject<OlmStorage>(fileName);
// work with the storage
Console::WriteLine(u"Top-level folders: " + Convert::ToString(olm->get_FolderHierarchy()->get_Count()));
olm->Dispose();
使用 FromFile 方法
静态 FromFile 方法在不构建文件夹树的情况下打开文件。当只需要全局存储操作 —— 如总项目计数、类别列表或按文件夹名称查找 —— 时,这是更轻量的选项,后续在 导航 OLM 文件夹.
String fileName = u"SampleOLM.olm";
SharedPtr<OlmStorage> olm = OlmStorage::FromFile(fileName);
// work with the storage
Console::WriteLine(u"Total items: " + Convert::ToString(olm->GetTotalItemsCount()));
olm->Dispose();
从流打开文件
如果存档来自数据库 Blob、网络资源或云存储桶,可直接从 System::IO::Stream。两者都 OlmStorage(stream) 构造函数和静态 FromStream 方法接受它,文件夹树初始化与基于文件的对应方法相同。
// The constructor accepts any readable stream
{
SharedPtr<IO::FileStream> stream = IO::File::OpenRead(u"SampleOLM.olm");
SharedPtr<OlmStorage> olm = MakeObject<OlmStorage>(stream);
Console::WriteLine(u"Top-level folders: " + Convert::ToString(olm->get_FolderHierarchy()->get_Count()));
// disposing the storage also closes the stream it was created from
olm->Dispose();
}
// The static factory method works the same way
{
SharedPtr<IO::FileStream> stream = IO::File::OpenRead(u"SampleOLM.olm");
SharedPtr<OlmStorage> olm = OlmStorage::FromStream(stream);
Console::WriteLine(u"Total items: " + Convert::ToString(olm->GetTotalItemsCount()));
olm->Dispose();
}
一旦从流创建,存储就拥有该流:
Dispose 也会关闭流。在存储使用期间请勿自行关闭流,并为每次新的 OlmStorage 实例。
打开受损文件
部分损坏的存档通常会在第一次遇到损坏项时中止。若要尽可能多地 salvaged,请使用以下方式创建存储: OlmStorage(TraversalExceptionsCallback) 构造函数并使用以下方式打开文件 Load。回调是一个 MulticastDelegate 接收每个 TraversalAsposeException 在读取文件时抛出,并附带导致错误的项目标识符,以便记录并跳过该失败项。 Load 返回 true 当文件可以打开并遍历时,且 false 当文件损坏到无法使用时。
String fileName = u"SampleOLM.olm";
// The callback receives every exception raised while the file is loaded or traversed
TraversalExceptionsCallback callback = std::function<void(TraversalAsposeException, String)>(
[](TraversalAsposeException exception, String id)
{
Console::WriteLine(u"Failed to process item '" + id + u"': " + exception->get_Message());
});
SharedPtr<OlmStorage> olm = MakeObject<OlmStorage>(callback);
if (olm->Load(fileName))
{
// The file was loaded and can be traversed
auto folderHierarchy = olm->GetFolders();
Console::WriteLine(u"Folders: " + Convert::ToString(folderHierarchy->get_Count()));
}
else
{
Console::WriteLine(u"The OLM file is corrupted and cannot be traversed.");
}
olm->Dispose();
一个完整示例展示了如何从此类文件中提取每个可读取的项目,示例位于 读取和提取 OLM 消息.