تقسیم فایل‌های MBOX

Very large MBOX files are slow to copy, back up and open. The SplitInto متد MboxStorageReader writes the messages of an archive into a series of part files of an approximate size that you choose. Messages are never cut in the middle: a part is closed once its size reaches the limit, so a part can end up somewhat larger than requested when a big message is appended to it.

نمونه‌های زیر از سرآیندها و فضاهای نام زیر استفاده می‌کنند:

#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;

Split an Archive into Parts

SplitInto takes the chunk size in bytes, the output directory and, optionally, a prefix for the part file names. Every time a new part is opened the reader raises the MboxFileCreated event; a handler connected to it receives a NewStorageEventArgs شیئی که FileName property names the file. The example splits an archive into parts of about 50 KB and prints the name of each part as it is created.

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();

Events in Aspose.Email for C++ are System::MulticastDelegate نمونه‌ها را انتظار دارد؛ یک handler با connect، که یک std::function با امضای رویداد.

لغو یک عملیات تقسیم

برای متوقف کردن تقسیم قبل از پردازش تمام آرشیو – برای مثال، وقتی فقط چند بخش اول لازم است، یا کاربر درخواست لغو می‌کند – فراخوانی کنید Cancel از داخل MboxFileCreated handler. بخش‌هایی که قبلاً نوشته شده‌اند بر روی دیسک می‌مانند؛ دیگر بخشی ایجاد نمی‌شود.

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();