MBOX ファイルの分割

非常に大きなMBOXファイルはコピー、バックアップ、オープンが遅くなります。The 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 プロパティはファイル名です。例ではアーカイブを約50KBのパートに分割し、作成されるたびに各パートの名前を出力します。

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