פיצול ומזוג קובצי PST
פיצול ומיזוג קבצי PST
קובץ PST שגדל ללא הפסקה הופך לאיטי לפתיחה, קשה לגיבוי ועלול להגיע למגבלת הגודל של גרסאות Outlook ישנות. Aspose.Email for C++ יכול לחתוך אחסון כזה למספר אחסונים קטנים יותר ובכיוון ההפך, לשלב מספר אחסונים לאחד. שני הפעולות עובדות ישירות על הקבצים, ללא 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 מפצל את תוכן האחסון על קבצי 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; כאשר הקידומת ריקה, שם התצוגה של אחסון המקור תופס את מקומו, מה שנותן שמות כגון Personal folders_part0.pst.
שני אירועים מדווחים מה הפעולה עושה. StorageProcessing מתרחש לפני כתיבת חלק ו StorageProcessed כאשר הוא מסתיים; השני גם מעביר את האחסון המוגמר דרך StorageProcessedEventArgs::Chunk, כך שניתן לבדוק חלק ללא פתיחת הקובץ שלו שוב. אותם אירועים מתרחשים בזמן MergeWith קורא את האחסונים שהוא ממזג.
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.
חילוק לפי קריטריון
גודל אינו הדרך היחידה לחתוך אחסון לחלקים. 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 מקבל מערך של נתיבי קבצים ומעתיק את תוכן כל אחד מהאחסונים האלה לתוך האחסון שעליו נקרא, ולכן הוא צריך להיות פתוח לכתיבה. תיקיות עם אותו נתיב משולבות לאחת, ותיקיות שקיימות רק באחד האחסונים מתווספות עם תת‑עץ מלא. ה‑ 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 מעתיק תיקייה אחת, עם כל מה שמתחתיה, מאחסון אחר לתוך התיקייה שעליה נקראת. בניגוד ל‑ PersonalStorage::MergeWith, שלוקח קבצים שלמים, מאפשר לבחור מה להעביר והיכן למקמו.
שני האחסונים נשארים פתוחים במהלך הפעולה: המקור רק נקראו, ולכן ניתן לפתוח אותו לקריאה‑רק, בעוד שהיעד צריך להיות בר שלכתיבה. ה‑ 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 אירוע נוסף מתרחש עבור הפריטים של תתי‑התיקיות שמגיעים עם תיקיית המקור.