OLM 파일 열기

모든 것은 an 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 속성이 준비되었습니다. 호출 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 메서드와 함께

정적 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();
}

손상된 파일 열기

부분적으로 손상된 아카이브는 일반적으로 첫 번째 손상된 항목을 만나면 중단됩니다. 가능한 한 많이 복구하려면 저장소를 다음과 같이 생성합니다 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 메시지 읽기 및 추출.