تقسيم ملفات MBOX

ملفات MBOX الضخمة جدًا بطيئة في النسخ والنسخ الاحتياطي والفتح. الـ SplitInto طريقة من MboxStorageReader يكتب رسائل الأرشيف في سلسلة من ملفات الأجزاء بحجم تقريبِي تختاره. لا تُقَطَع الرسائل في المنتصف: يُغلق الجزء بمجرد وصول حجمه إلى الحد، لذا قد يصبح حجمه أكبر قليلًا عندما تُضاف رسالة كبيرة إليه.

العينات أدناه تستخدم الرؤوس والمساحات الاسمية التالية:

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

تقسيم أرشيف إلى أجزاء

SplitInto يأخذ حجم الجزء بالبايت، دليل الإخراج و، اختياريًا، بادئة لأسماء ملفات الأجزاء. كلما فُتِح جزء جديد يرفع القارئ الحدث MboxFileCreated الحدث؛ المعالج المتصل به يتلقى NewStorageEventArgs الكائن الذي يملك FileName اسم الخاصية للملف. المثال يقسم الأرشيف إلى أجزاء بحجم تقريبي 50 كيلوبايت ويطبع اسم كل جزء عند إنشائه.

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

الأحداث في Aspose.Email للـ C++ هي System::MulticastDelegate مثيلات؛ يُربط معالج بـ connect، الذي يتوقع std::function مع توقيع الحدث.

إلغاء عملية التقسيم

لإيقاف التقسيم قبل معالجة الأرشيف بالكامل - على سبيل المثال، عندما تكون الحاجة فقط لبعض الأجزاء الأولى، أو عندما يطلب المستخدم الإلغاء - استدعِ إلغاء من داخل الـ MboxFileCreated المعالج. الأجزاء التي كُتبت بالفعل تبقى على القرص؛ لا تُنشأ أجزاء إضافية.

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