Exporting and Extracting Zimbra TGZ Content
Reading a backup is rarely the goal in itself - the messages usually have to end up somewhere else. TgzReader supports two approaches: a one-call export that mirrors the mailbox on disk, and manual saving of whichever messages you pick while iterating.
The samples below use the following headers and namespaces:
#include <MailMessage.h>
#include <SaveOptions.h>
#include <EmlSaveOptions.h>
#include <MsgSaveOptions.h>
#include <AttachmentCollection.h>
#include <Storage/Zimbra/TgzReader.h>
#include <system/io/directory.h>
#include <system/string_comparison.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Storage::Zimbra;
Export the Whole Mailbox
ExportTo takes a directory path and writes every item of the backup below it, creating a sub-directory for each Zimbra folder. Messages are saved in their original MIME form, and non-mail items such as contacts are written in their own formats (a contact becomes a .vcf file). The target directory has to exist.
SharedPtr<TgzReader> reader = MakeObject<TgzReader>(u"ZimbraSample.tgz");
// the target directory must exist and be writable
IO::Directory::CreateDirectory_(u"ZimbraExport");
// recreate the mailbox folder tree on disk and write every message into it
reader->ExportTo(u"ZimbraExport");
reader->Dispose();
Save Selected Messages
When only part of the mailbox is wanted, iterate as described in Reading Zimbra TGZ Files and save the CurrentMessage yourself. Because it is a MailMessage, any SaveOptions can be applied. The example keeps only the items whose folder path starts with Inbox and writes them as EML:
SharedPtr<TgzReader> reader = MakeObject<TgzReader>(u"ZimbraSample.tgz");
IO::Directory::CreateDirectory_(u"ZimbraInbox");
int32_t index = 0;
do
{
SharedPtr<MailMessage> message = reader->get_CurrentMessage();
if (message == nullptr)
{
continue;
}
// keep only the items that belong to the Inbox folder
if (reader->get_CurrentDirectory().StartsWith(u"Inbox", StringComparison::OrdinalIgnoreCase))
{
message->Save(String::Format(u"ZimbraInbox\\message_{0}.eml", index++), SaveOptions::get_DefaultEml());
}
}
while (reader->ReadNextMessage());
reader->Dispose();
Filter by Message Properties
Any property of the message can drive the decision. Here the messages that carry attachments are saved in Outlook MSG format by passing SaveOptions::DefaultMsgUnicode:
SharedPtr<TgzReader> reader = MakeObject<TgzReader>(u"ZimbraSample.tgz");
IO::Directory::CreateDirectory_(u"ZimbraWithAttachments");
int32_t index = 0;
do
{
SharedPtr<MailMessage> message = reader->get_CurrentMessage();
if (message != nullptr && message->get_Attachments()->get_Count() > 0)
{
// Outlook MSG format this time
message->Save(String::Format(u"ZimbraWithAttachments\\message_{0}.msg", index++), SaveOptions::get_DefaultMsgUnicode());
}
}
while (reader->ReadNextMessage());
reader->Dispose();