拆分 MBOX 文件
Contents
[
Hide
]
非常大的 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 KB 的部件,并在每个部件创建时打印其名称。
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();