การเปิดไฟล์ OLM

ทุกอย่างเริ่มต้นด้วย 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 เมธอดนี้เปิดไฟล์โดยไม่สร้างต้นไม้โฟลเดอร์ เป็นตัวเลือกที่เบากว่าเมื่อคุณต้องการทำงานทั่ว storage เท่านั้น - การนับรายการทั้งหมด, รายการหมวดหมู่, หรือการค้นหาตามชื่อโฟลเดอร์ - และอธิบายเพิ่มเติมใน การนำทางโฟลเดอร์ 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 ของฐานข้อมูล, ทรัพยากรเครือข่ายหรือ bucket คลาวด์, ให้เปิดโดยตรงจาก 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();
}

เปิดไฟล์ที่เสีย

ไฟล์เก็บที่เสียหายบางส่วนมักจะหยุดทำงานเมื่อเจอรายการที่เสียครั้งแรก. เพื่อตัดสินใจกู้ข้อมูลให้มากที่สุด, สร้างที่เก็บด้วย 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.