การแยกไฟล์ 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 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 สำหรับ 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();