Converting Zimbra TGZ to PST
Contents
[
Hide
]
A Zimbra backup already carries the information needed to reproduce the mailbox in Outlook: each item comes with the path of its folder, and each message is standard MIME. Turning it into a PST therefore boils down to creating the folders and converting the messages to MAPI. No Zimbra server or Outlook installation is involved.
The sample below uses the following headers and namespaces:
#include <MailMessage.h>
#include <Mapi/MapiMessage.h>
#include <Storage/Zimbra/TgzReader.h>
#include <Storage/Pst/PersonalStorage.h>
#include <Storage/Pst/FolderInfo.h>
#include <Storage/Pst/FileFormatVersion.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::Storage::Pst;
using namespace Aspose::Email::Storage::Zimbra;
Conversion Outline
- Open the backup with TgzReader.
- Create the output file with PersonalStorage::Create.
- For every item, translate CurrentDirectory into a PST folder, creating the missing levels on the way.
- Convert CurrentMessage with MapiMessage::FromMailMessage and add it to that folder with FolderInfo::AddMessage.
Folder Lookup Helper
CurrentDirectory uses a forward slash between path segments (Inbox/Projects/2023). The helper splits the path, walks down from the RootFolder and creates every segment that does not exist yet with AddSubFolder:
// Finds the PST folder that corresponds to a Zimbra directory path, creating missing levels
SharedPtr<FolderInfo> GetOrCreateFolder(SharedPtr<PersonalStorage> pst, String directory)
{
SharedPtr<FolderInfo> folder = pst->get_RootFolder();
if (String::IsNullOrEmpty(directory))
{
return folder;
}
// the path uses "/" as separator, e.g. Inbox/Projects/2023
for (String name : directory.Split(u'/', StringSplitOptions::RemoveEmptyEntries))
{
SharedPtr<FolderInfo> subFolder = folder->GetSubFolder(name);
folder = subFolder != nullptr ? subFolder : folder->AddSubFolder(name);
}
return folder;
}
Run the Conversion
// open the source backup
SharedPtr<TgzReader> reader = MakeObject<TgzReader>(u"ZimbraSample.tgz");
// create the target PST file
SharedPtr<PersonalStorage> pst = PersonalStorage::Create(u"FromZimbra.pst", FileFormatVersion::Unicode);
do
{
SharedPtr<MailMessage> message = reader->get_CurrentMessage();
if (message == nullptr)
{
continue;
}
// find or create the folder that matches the directory of the current item
SharedPtr<FolderInfo> folder = GetOrCreateFolder(pst, reader->get_CurrentDirectory());
// PST folders store messages in MAPI format
folder->AddMessage(MapiMessage::FromMailMessage(message));
}
while (reader->ReadNextMessage());
pst->Dispose();
reader->Dispose();
FromMailMessage carries over the standard fields, the body and the attachments of each message. Zimbra-specific metadata that has no MAPI counterpart (tags, flags stored outside the MIME data) is not transferred.