การแยกและรวมไฟล์ PST
การแบ่งและรวมไฟล์ PST
ไฟล์ PST ที่ขยายใหญ่ขึ้นเรื่อย ๆ จะเปิดได้ช้า สำรองข้อมูลยากและอาจถึงขีดจำกัดขนาดของ Outlook เวอร์ชันเก่า Aspose.Email สำหรับ C++ สามารถตัด storage แบบนี้เป็นหลายส่วนที่เล็กลงและกลับกันรวมหลาย storage เป็นหนึ่งส่วน ทั้งสองการทำงานทำงานกับไฟล์โดยตรงโดยไม่ต้องใช้ Outlook และรายงานความคืบหน้าผ่านเหตุการณ์
ตัวอย่างในบทความนี้ใช้ส่วนหัวและเนมสเปซต่อไปนี้:
#include <Mapi/MapiMessage.h>
#include <Storage/Pst/PersonalStorage.h>
#include <Storage/Pst/FolderInfo.h>
#include <Storage/Pst/FileFormatVersion.h>
#include <Storage/Pst/ItemMovedEventArgs.h>
#include <Storage/Pst/StorageProcessedEventArgs.h>
#include <Storage/Pst/StorageProcessingEventArgs.h>
#include <Storage/Pst/MessageStore.h>
#include <Storage/Pst/PersonalStorageQueryBuilder.h>
#include <Tools/Search/MailQuery.h>
#include <Tools/Search/StringComparisonField.h>
#include <system/array.h>
#include <system/convert.h>
#include <system/collections/list.h>
#include <system/io/directory.h>
#include <system/io/path.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Storage::Pst;
using namespace Aspose::Email::Tools::Search;
การแบ่งเป็นหลายไฟล์ PST
PersonalStorage::SplitInto กระจายเนื้อหา storage ไปยังไฟล์ PST ใหม่ที่มีขนาดประมาณที่กำหนด (หน่วยไบต์) และเขียนลงในไดเรกทอรีที่ระบุ แต่ละส่วนรักษาโครงสร้างโฟลเดอร์ของต้นทาง และไฟล์ต้นทางจะไม่ถูกแก้ไข จึงสามารถเปิดแบบอ่านอย่างเดียวได้ ไฟล์ส่วนที่มีอยู่แล้วจะไม่ถูกเขียนทับ – คำร้องจะล้มเหลวแทน – ดังนั้นไดเรกทอรีเป้าหมายควรว่างเปล่า
// SplitInto does not overwrite the parts of an earlier run, so start with an empty directory
if (IO::Directory::Exists(u"Parts"))
{
IO::Directory::Delete(u"Parts", true);
}
IO::Directory::CreateDirectory_(u"Parts");
auto pst = PersonalStorage::FromFile(u"demo.pst", false);
// parts of about 5 MB each
pst->SplitInto(static_cast<int64_t>(5000000), u"Parts");
pst->Dispose();
for (String file : IO::Directory::GetFiles(u"Parts", u"*.pst"))
{
Console::WriteLine(u"Part written: " + IO::Path::GetFileName(file));
}
ตั้งชื่อส่วนและติดตามความคืบหน้า
การโหลดเกิน SplitInto(chunkSize, partFileNamePrefix, path) ควบคุมวิธีตั้งชื่อส่วน แต่ละไฟล์จะถูกเรียกว่า {prefix}part{number}.pst; เมื่อพรีฟิกซ์ว่างเปล่า ชื่อแสดงของ storage ต้นทางจะเข้ามาแทน ซึ่งให้ชื่อเช่น Personal folders_part0.pst.
สองเหตุการณ์รายงานว่าการดำเนินการกำลังทำอะไร StorageProcessing ถูกยกขึ้นก่อนที่ส่วนจะถูกเขียนและ StorageProcessed เมื่อเสร็จแล้ว; ตัวที่สองยังส่ง storage ที่เสร็จสิ้นผ่าน StorageProcessedEventArgs::Chunk, ดังนั้นส่วนหนึ่งสามารถตรวจสอบได้โดยไม่ต้องเปิดไฟล์อีกครั้ง เหตุการณ์เดียวกันจะถูกยิงขณะ MergeWith อ่าน storage ที่มันผสาน
if (IO::Directory::Exists(u"PartsBySize"))
{
IO::Directory::Delete(u"PartsBySize", true);
}
IO::Directory::CreateDirectory_(u"PartsBySize");
auto pst = PersonalStorage::FromFile(u"demo.pst", false);
// raised before a part is started
pst->StorageProcessing.connect(
std::function<void(System::SharedPtr<System::Object>, System::SharedPtr<StorageProcessingEventArgs>)>(
[](System::SharedPtr<System::Object> sender, System::SharedPtr<StorageProcessingEventArgs> e)
{
Console::WriteLine(u"Writing " + e->get_FileName());
}));
// raised when a part is complete; Chunk gives access to the storage that was just written
pst->StorageProcessed.connect(
std::function<void(System::SharedPtr<System::Object>, System::SharedPtr<StorageProcessedEventArgs>)>(
[](System::SharedPtr<System::Object> sender, System::SharedPtr<StorageProcessedEventArgs> e)
{
Console::WriteLine(u"Finished " + e->get_FileName() + u", items: " +
Convert::ToString(e->get_Chunk()->get_Store()->GetTotalItemsCount()));
}));
pst->SplitInto(static_cast<int64_t>(5000000), u"chunk_", u"PartsBySize");
pst->Dispose();
ArgumentException.
การตัดตามเกณฑ์
ขนาดไม่ใช่วิธีเดียวที่จะตัด storage SplitInto(criteria, path) รับรายการคำค้นแทนขนาดและเขียนหนึ่งส่วนต่อคำค้น โดยเก็บข้อความที่ตรงกับคำค้นนั้น ข้อความที่ไม่ตรงกับคำค้นใดเลยจะไม่ถูกใส่ในส่วนใดส่วนหนึ่ง ดังนั้นเกณฑ์จึงทำหน้าที่เป็นฟิลเตอร์ – วิธีที่สะดวกในการดึงผู้ส่งหนึ่งคน ช่วงวันที่หนึ่ง หรือโฟลเดอร์หนึ่งจากกล่องเมล์ขนาดใหญ่
ส่วนต่าง ๆ รักษาโครงสร้างโฟลเดอร์ของต้นทาง และไฟล์ต้นทางไม่ได้ถูกแก้ไข
if (IO::Directory::Exists(u"PartsByCriteria"))
{
IO::Directory::Delete(u"PartsByCriteria", true);
}
IO::Directory::CreateDirectory_(u"PartsByCriteria");
// one query per part; a message that matches none of them is not copied anywhere
auto criteria = System::MakeObject<System::Collections::Generic::List<System::SharedPtr<MailQuery>>>();
auto firstSender = System::MakeObject<PersonalStorageQueryBuilder>();
firstSender->get_From()->Contains(u"erin@example.com", true);
criteria->Add(firstSender->GetQuery());
auto secondSender = System::MakeObject<PersonalStorageQueryBuilder>();
secondSender->get_From()->Contains(u"frank@example.com", true);
criteria->Add(secondSender->GetQuery());
auto pst = PersonalStorage::FromFile(u"source.pst", false);
pst->SplitInto(criteria, u"PartsByCriteria");
pst->Dispose();
for (String file : IO::Directory::GetFiles(u"PartsByCriteria", u"*.pst"))
{
Console::WriteLine(IO::Path::GetFileName(file));
}
การรวมเป็น PSTเดียว
PersonalStorage::MergeWith รับอาร์เรย์ของเส้นทางไฟล์และคัดลอกเนื้อหาของแต่ละ storage เหล่านั้นเข้าไปใน storage ที่เรียกใช้ ซึ่งต้องเปิดแบบเขียนได้ โฟลเดอร์ที่มีเส้นทางเดียวกันจะถูกรวมเป็นหนึ่งโฟลเดอร์ และโฟลเดอร์ที่มีเพียงใน storage หนึ่งจะถูกเพิ่มพร้อมต้นไม้ย่อยทั้งหมด ส่วน StorageProcessed เหตุการณ์รายงานไฟล์ต้นทางแต่ละครั้งเมื่ออ่านเสร็จ
auto merged = PersonalStorage::Create(u"Merged.pst", FileFormatVersion::Unicode);
merged->StorageProcessed.connect(
std::function<void(System::SharedPtr<System::Object>, System::SharedPtr<StorageProcessedEventArgs>)>(
[](System::SharedPtr<System::Object> sender, System::SharedPtr<StorageProcessedEventArgs> e)
{
Console::WriteLine(u"Merged " + IO::Path::GetFileName(e->get_FileName()));
}));
merged->MergeWith(System::MakeArray<String>({u"demo.pst", u"source.pst"}));
Console::WriteLine(u"Items in the merged storage: " + Convert::ToString(merged->get_Store()->GetTotalItemsCount()));
merged->Dispose();
การรวมโฟลเดอร์จาก PST อื่น
FolderInfo::MergeWith คัดลอกโฟลเดอร์เดียวพร้อมทุกอย่างด้านล่างจาก storage อื่นไปยังโฟลเดอร์ที่เรียกใช้ ไม่เหมือน PersonalStorage::MergeWith, ซึ่งรับไฟล์ทั้งหมด วิธีนี้ให้คุณเลือกว่าจะนำอะไรไปและจะวางที่ไหน
ทั้งสอง storage ยังคงเปิดอยู่ระหว่างการทำงาน: แหล่งที่มาอ่านอย่างเดียวจึงอาจเปิดแบบอ่าน‑อย่าง‑เดียวได้ ในขณะที่ปลายทางต้องเปิดแบบเขียนได้ ItemMoved เหตุการณ์ของโฟลเดอร์ปลายทางรายงานแต่ละรายการเมื่อมาถึงและ ItemMovedEventArgs แยกข้อความและโฟลเดอร์ออกจากกัน
auto destination = PersonalStorage::FromFile(u"demo.pst", true);
auto source = PersonalStorage::FromFile(u"source.pst", false);
auto target = destination->get_RootFolder()->AddSubFolder(u"FolderFromAnotherPst");
auto imported = source->get_RootFolder()->GetSubFolder(u"Imported");
// optional: report every item as it arrives
target->ItemMoved.connect(
std::function<void(System::SharedPtr<System::Object>, System::SharedPtr<ItemMovedEventArgs>)>(
[](System::SharedPtr<System::Object> sender, System::SharedPtr<ItemMovedEventArgs> e)
{
Console::WriteLine(u"Merged an item, message: " + Convert::ToString(e->get_IsMessage()) +
u", folder: " + Convert::ToString(e->get_IsFolder()));
}));
// the source folder and its whole subtree are copied into the target folder
target->MergeWith(imported);
Console::WriteLine(u"The merged folder holds " +
Convert::ToString(destination->get_RootFolder()->GetSubFolder(u"FolderFromAnotherPst")->get_ContentCount()) +
u" messages");
source->Dispose();
destination->Dispose();
การโหลดเกินที่สอง, MergeWith(sourceFolder, recursiveHandler), ตัดสินใจว่าที่ ItemMoved เหตุการณ์ยังถูกยกขึ้นสำหรับรายการของโฟลเดอร์ย่อยที่มาพร้อมกับโฟลเดอร์ต้นทาง