Разделяне на MBOX файлове
Много големите MBOX файлове са бавни за копиране, архивиране и отваряне. 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();
Събитията в Aspose.Email за C++ са System::MulticastDelegate екземпляри; обработчик се прикрепя с connect, което очаква std::function с подписа на събитието.
Отказ от операция за разделяне
За да спрете разделянето преди целият архив да бъде обработен – например, когато са нужни само първите няколко части, или когато потребителят иска да прекъсне – извикайте Cancel отвътре в 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();