Opening OLM Files
Everything starts with an OlmStorage object. It can be created from a file path, from any readable stream, or - for files that may be damaged - through a constructor that takes an error callback. This article covers all three ways.
The samples below use the following headers and namespaces:
#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;
Open a File by Path
With the Constructor
Passing the file name to the OlmStorage constructor opens the archive and reads its folder tree immediately, so the FolderHierarchy property is ready to use. Call Dispose when you are done to release the file handle.
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();
With the FromFile Method
The static FromFile method opens the file without building the folder tree. It is the lighter option when you only need a storage-wide operation - a total item count, the category list, or a lookup by folder name - and it is described further in Navigating OLM Folders.
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();
Open a File from a Stream
If the archive arrives from a database blob, a network resource or a cloud bucket, open it straight from a System::IO::Stream. Both the OlmStorage(stream) constructor and the static FromStream method accept it, with the same difference in folder-tree initialization as their file-based counterparts.
// 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 closes the stream as well. Do not close the stream yourself while the storage is in use, and open a fresh stream for every new OlmStorage instance.
Open a Damaged File
A partially corrupted archive would normally abort the first time a broken item is met. To salvage as much as possible instead, create the storage with the OlmStorage(TraversalExceptionsCallback) constructor and open the file with Load. The callback is a MulticastDelegate that receives every TraversalAsposeException raised while the file is read, together with the identifier of the item that caused it, so the failure can be logged and skipped. Load returns true when the file could be opened and traversed, and false when it is too damaged to work with.
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();
A full example that extracts every readable item from such a file is given in Reading and Extracting OLM Messages.