Reading Zimbra TGZ Files
TgzReader decompresses and parses a Zimbra backup on the fly, one item at a time, so even multi-gigabyte exports can be processed with a small memory footprint. This article shows how to create the reader, move through the archive and inspect what the reader is positioned on.
The samples below use the following headers and namespaces:
#include <MailMessage.h>
#include <MailAddress.h>
#include <MailAddressCollection.h>
#include <AttachmentCollection.h>
#include <Storage/Zimbra/TgzReader.h>
#include <system/io/file.h>
#include <system/convert.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Storage::Zimbra;
Open a Backup from a File
Pass the path to the TgzReader constructor. The reader positions itself on the first item straight away, which is why CurrentDirectory can be read immediately. Call Dispose to release the file when finished.
String fileName = u"ZimbraSample.tgz";
SharedPtr<TgzReader> reader = MakeObject<TgzReader>(fileName);
// work with the backup
Console::WriteLine(u"First folder: " + reader->get_CurrentDirectory());
reader->Dispose();
Open a Backup from a Stream
Any readable System::IO::Stream works as a source - a file opened elsewhere, a memory buffer, a network download. Keep the stream open until the reader has been disposed.
SharedPtr<IO::FileStream> stream = IO::File::OpenRead(u"ZimbraSample.tgz");
SharedPtr<TgzReader> reader = MakeObject<TgzReader>(stream);
// work with the backup
Console::WriteLine(u"Items: " + Convert::ToString(reader->GetTotalItemsCount()));
reader->Dispose();
stream->Close();
Iterate over the Items
ReadNextMessage advances to the next item and returns false at the end of the archive. Since the reader already points at the first item after construction, a do ... while loop processes that item before moving on. CurrentMessage is nullptr for archive entries that are not mail messages (a contact stored in the backup, for example), so test it before use.
SharedPtr<TgzReader> reader = MakeObject<TgzReader>(u"ZimbraSample.tgz");
// the reader is already positioned on the first item, so process it before advancing
do
{
Console::WriteLine(u"Folder: " + reader->get_CurrentDirectory());
SharedPtr<MailMessage> message = reader->get_CurrentMessage();
if (message != nullptr)
{
Console::WriteLine(u"Subject: " + message->get_Subject());
}
}
while (reader->ReadNextMessage());
reader->Dispose();
Read Message Properties
The current item is an ordinary MailMessage, so subject, addresses, date, body and attachments are all available while iterating:
SharedPtr<TgzReader> reader = MakeObject<TgzReader>(u"ZimbraSample.tgz");
do
{
SharedPtr<MailMessage> message = reader->get_CurrentMessage();
if (message == nullptr)
{
continue;
}
Console::WriteLine(u"Folder: " + reader->get_CurrentDirectory());
Console::WriteLine(u"Subject: " + message->get_Subject());
Console::WriteLine(u"From: " + message->get_From()->ToString());
Console::WriteLine(u"To: " + message->get_To()->ToString());
Console::WriteLine(u"Date: " + message->get_Date().ToString());
Console::WriteLine(u"Attachments: " + Convert::ToString(message->get_Attachments()->get_Count()));
Console::WriteLine(String(u'-', 30));
}
while (reader->ReadNextMessage());
reader->Dispose();
Count the Items
GetTotalItemsCount returns the number of items in the backup. Combined with a counter inside the loop it gives a simple “n of total” progress indicator:
SharedPtr<TgzReader> reader = MakeObject<TgzReader>(u"ZimbraSample.tgz");
int32_t total = reader->GetTotalItemsCount();
Console::WriteLine(u"The backup contains " + Convert::ToString(total) + u" items.");
// combine the count with the iteration to report progress
int32_t processed = 0;
do
{
processed++;
SharedPtr<MailMessage> message = reader->get_CurrentMessage();
String subject = message != nullptr ? message->get_Subject() : String(u"<no message>");
Console::WriteLine(String::Format(u"Processing item {0} of {1}: {2}", processed, total, subject));
}
while (reader->ReadNextMessage());
reader->Dispose();