Mở Tập tin OLM
Mọi thứ bắt đầu với một OlmStorage đối tượng. Nó có thể được tạo từ một đường dẫn tệp, từ bất kỳ luồng đọc được nào, hoặc - đối với các tệp có thể bị hỏng - thông qua một bộ khởi tạo nhận một callback lỗi. Bài viết này bao gồm cả ba cách.
Các mẫu dưới đây sử dụng các tiêu đề và không gian tên sau:
#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;
Mở Tệp Bằng Đường Dẫn
Với Bộ Khởi Tạo
Truyền tên tệp cho OlmStorage bộ khởi tạo mở kho lưu trữ và đọc cây thư mục của nó ngay lập tức, vì vậy FolderHierarchy tài sản đã sẵn sàng để sử dụng. Gọi Giải phóng khi bạn hoàn thành để giải phóng handle tệp.
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();
Với Phương thức FromFile
Phương thức tĩnh FromFile phương thức mở tệp mà không xây dựng cây thư mục. Đây là lựa chọn nhẹ hơn khi bạn chỉ cần một thao tác trên toàn bộ lưu trữ - đếm tổng số mục, danh sách danh mục, hoặc tra cứu theo tên thư mục - và nó được mô tả chi tiết hơn trong Duyệt Thư mục 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();
Mở Tệp từ một Luồng
Nếu kho lưu trữ đến từ blob cơ sở dữ liệu, tài nguyên mạng hoặc bucket đám mây, mở nó trực tiếp từ một System::IO::Stream. Cả OlmStorage(stream) constructor và static FromStream phương thức chấp nhận nó, với sự khác biệt tương tự trong việc khởi tạo cây thư mục như các đối tác dựa trên tệp.
// 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 đóng luồng cũng vậy. Đừng tự mình đóng luồng khi bộ lưu trữ đang được sử dụng, và mở một luồng mới cho mỗi lần OlmStorage thể hiện.
Mở Tệp Bị Hỏng
Một kho lưu trữ bị hỏng một phần thường sẽ dừng lại lần đầu gặp mục bị hỏng. Để cứu càng nhiều càng tốt, thay vào đó hãy tạo kho lưu trữ với OlmStorage(TraversalExceptionsCallback) bộ khởi tạo và mở tệp bằng Load. Callback là một MulticastDelegate nhận mỗi TraversalAsposeException được ném khi tệp đang được đọc, cùng với định danh của mục gây ra lỗi, vì vậy sự cố có thể được ghi lại và bỏ qua. Load trả về true khi tệp có thể được mở và duyệt, và false khi nó bị hỏng quá mức để có thể làm việc.
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();
Một ví dụ đầy đủ trích xuất mọi mục có thể đọc được từ tệp như vậy được đưa ra trong Đọc và Trích xuất Tin nhắn OLM.