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 이벤트는 또한 소스 폴더와 함께 제공되는 하위 폴더의 항목에 대해 발생합니다.