Chia tách các tệp MBOX

Các tệp MBOX rất lớn chậm sao chép, sao lưu và mở. SplitInto phương thức của MboxStorageReader ghi các tin trong một kho lưu trữ vào một loạt tệp phần có kích thước gần với giá trị bạn chọn. Tin nhắn không bao giờ bị cắt giữa chừng: một phần sẽ đóng khi kích thước đạt giới hạn, vì vậy một phần có thể hơi lớn hơn khi một tin lớn được thêm vào.

Các mẫu dưới đây sử dụng các tiêu đề và không gian tên sau:

#include <Storage/Mbox/MboxrdStorageReader.h>
#include <Storage/Mbox/MboxLoadOptions.h>
#include <Storage/NewStorageEventArgs.h>
#include <system/io/directory.h>
#include <system/convert.h>

using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Storage;
using namespace Aspose::Email::Storage::Mbox;

Tách một kho lưu trữ thành các phần

SplitInto nhận kích thước đoạn tính bằng byte, thư mục đầu ra và, tùy chọn, tiền tố cho tên tệp phần. Mỗi khi một phần mới được mở, bộ đọc phát sinh MboxFileCreated sự kiện; một trình xử lý được kết nối sẽ nhận một NewStorageEventArgs đối tượng mà FileName đặt tên file. Ví dụ này tách một kho lưu trữ thành các phần khoảng 50 KB và in tên mỗi phần khi nó được tạo.

SharedPtr<MboxLoadOptions> options = MakeObject<MboxLoadOptions>();
options->set_LeaveOpen(false);
SharedPtr<MboxrdStorageReader> reader = MakeObject<MboxrdStorageReader>(u"mailbox.mbox", options);

IO::Directory::CreateDirectory_(u"Chunks");

// Get notified every time a new part file is created
reader->MboxFileCreated.connect(std::function<void(SharedPtr<Object>, SharedPtr<NewStorageEventArgs>)>(
    [](SharedPtr<Object> sender, SharedPtr<NewStorageEventArgs> e)
    {
        Console::WriteLine(u"Created: " + e->get_FileName());
    }));

// Split the archive into parts of roughly 50 KB each, using the "part_" file name prefix
reader->SplitInto(50 * 1024, u"Chunks", u"part_");

reader->Dispose();

Sự kiện trong Aspose.Email cho C++ là System::MulticastDelegate đối tượng; một trình xử lý được gắn với connect, mà yêu cầu một std::function với chữ ký của sự kiện.

Hủy một thao tác tách

Để dừng việc tách trước khi toàn bộ kho lưu trữ được xử lý - ví dụ, khi chỉ cần vài phần đầu, hoặc khi người dùng yêu cầu hủy - gọi Hủy từ bên trong MboxFileCreated trình xử lý. Các phần đã được ghi sẽ ở lại đĩa; không tạo thêm phần nào.

int32_t partCount = 0;

SharedPtr<MboxLoadOptions> options = MakeObject<MboxLoadOptions>();
options->set_LeaveOpen(false);
SharedPtr<MboxrdStorageReader> reader = MakeObject<MboxrdStorageReader>(u"mailbox.mbox", options);

IO::Directory::CreateDirectory_(u"Chunks-Cancel");

// Stop after the second part has been created
reader->MboxFileCreated.connect(std::function<void(SharedPtr<Object>, SharedPtr<NewStorageEventArgs>)>(
    [&partCount, reader](SharedPtr<Object> sender, SharedPtr<NewStorageEventArgs> e)
    {
        partCount++;
        if (partCount >= 2)
        {
            reader->Cancel();
        }
    }));

reader->SplitInto(20 * 1024, u"Chunks-Cancel");
Console::WriteLine(u"Parts created: " + Convert::ToString(partCount));

reader->Dispose();