PST ファイルの分割と結合

PST ファイルの分割と結合

サイズが大きくなると開くのが遅くなり、バックアップが困難になり、古い Outlook バージョンのサイズ制限に達することがあります。Aspose.Email for C++ はそのようなストレージをいくつかの小さなストレージに分割し、逆に複数のストレージを 1 つに結合できます。両方の操作は 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.

2つのイベントが操作の進行状況を報告します。 StorageProcessing パートが書き込まれる前に発生し、 StorageProcessed 完了したら;2つ目のイベントは完了したストレージを通じても渡します 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();

基準による分割

サイズだけがストレージを分割する唯一の方法ではありません。 SplitInto(criteria, path) サイズの代わりにクエリのリストを受け取り、クエリごとに 1 つのパートを書き込み、そのクエリに一致するメッセージを保持します。いずれのクエリにも一致しないメッセージはすべてのパートから除外されるため、条件はフィルタとしても機能します——大量メールボックスから送信者や日付範囲、フォルダ単位で抽出する便利な方法です。

パーツはソースのフォルダ構造を保持し、ソースファイル自体は変更されません。

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 ファイルパスの配列を受け取り、各ストレージの内容を呼び出し元にコピーします。したがって呼び出し側は書き込み可能である必要があります。同じパスのフォルダは 1 つに統合され、片方にしか存在しないフォルダはそのサブツリー全体が追加されます。 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();

2番目のオーバーロード、 MergeWith(sourceFolder, recursiveHandler)、が決めます ItemMoved イベントは、ソースフォルダに付随するサブフォルダのアイテムについても発生します。