OLM ファイルのオープン
すべては OlmStorage オブジェクト。ファイルパス、任意の読み取り可能ストリーム、または破損した可能性があるファイル用にエラ―コールバックを受け取るコンストラクタから作成できます。本記事ではこの3つの方法すべてを取り上げます。
以下のサンプルは次のヘッダーと名前空間を使用しています:
#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 プロパティは使用可能です。呼び出し Dispose 完了したらファイルハンドルを解放します。
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 メソッドを使用する場合
static 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) コンストラクタと static 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 インスタンス。
破損ファイルを開く
部分的に破損したアーカイブは通常、最初に壊れたアイテムに遭遇した時点で中止します。できるだけ多く回復するには、次のものを使ってストレージを作成します 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 メッセージの読み取りと抽出.