Creating Zimbra TGZ Backups from EML Files
Aspose.Email for C++ can build a Zimbra-compatible TGZ archive from a collection of EML files. This simplifies backup and migration workflows: for example, you can collect messages exported from another mail system and import them into Zimbra as a single backup.
Create a TGZ Backup from a Directory of EML Files
The static TgzBackupBuilder::Build() method of the Aspose::Email::Storage::Zimbra::TgzBackup namespace takes three parameters:
inputDirectory- the directory with the*.emlfiles. The directory is scanned recursively, and files with other extensions are ignored.outputArchive- the path of the TGZ archive to create.rootFolderName- the name of the top-level mailbox folder for the imported messages. Subdirectories of the input directory become its subfolders, so the files fromemails/Projectsare placed in theProjectssubfolder ofImported.
The method returns a BuildResult object with the details of the operation: get_MessagesWritten() returns the number of messages added to the archive, and get_Errors() returns the list of errors that occurred while building it.
#include <Storage/Zimbra/TgzBackup/TgzBackupBuilder.h>
#include <Storage/Zimbra/TgzBackup/BuildResult.h>
#include <system/collections/ilist.h>
#include <system/console.h>
#include <system/convert.h>
using namespace System;
using namespace Aspose::Email::Storage::Zimbra::TgzBackup;
SharedPtr<BuildResult> result = TgzBackupBuilder::Build(u"emails", u"backup.tgz", u"Imported");
Console::WriteLine(u"Messages written: " + Convert::ToString(result->get_MessagesWritten()));
for (const String& error : result->get_Errors())
{
Console::WriteLine(u"Error: " + error);
}
Read the Created Backup
The resulting archive can be read with the TgzReader class like any other Zimbra backup. The reader is positioned on the first item after construction, so a do ... while loop processes it before ReadNextMessage() moves to the next one. get_CurrentDirectory() returns the mail folder of the current item, and get_CurrentMessage() returns the message itself.
#include <MailMessage.h>
#include <Storage/Zimbra/TgzReader.h>
#include <system/console.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Storage::Zimbra;
auto reader = MakeObject<TgzReader>(u"backup.tgz");
do
{
SharedPtr<MailMessage> message = reader->get_CurrentMessage();
if (message != nullptr)
{
Console::WriteLine(reader->get_CurrentDirectory() + u": " + message->get_Subject());
}
} while (reader->ReadNextMessage());
reader->Dispose();