Creating Zimbra TGZ Backups from EML Files

Aspose.Email for Python via .NET 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 build method of the TgzBackupBuilder class from the aspose.email.storage.zimbra.tgzbackup module takes three parameters:

  • input_directory - the directory with the *.eml files. The directory is scanned recursively, and files with other extensions are ignored.
  • output_archive - the path of the TGZ archive to create.
  • root_folder_name - the name of the top-level mailbox folder for the imported messages. Subdirectories of the input directory become its subfolders, so the files from emails/Projects are placed in the Projects subfolder of Imported.

The method returns a BuildResult object with the details of the operation: messages_written is the number of messages added to the archive, and errors is the list of errors that occurred while building it.

import aspose.email as ae

result = ae.storage.zimbra.tgzbackup.TgzBackupBuilder.build("emails", "backup.tgz", "Imported")

print("Messages written:", result.messages_written)
for error in result.errors:
    print("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 it is created, so the loop below processes that item before the read_next_message method moves to the next one. The current_directory property returns the mail folder of the current item, and current_message returns the message itself (see the TgzReader properties).

import aspose.email as ae

with ae.storage.zimbra.TgzReader("backup.tgz") as reader:
    while True:
        message = reader.current_message
        if message is not None:
            print(f"{reader.current_directory}: {message.subject}")
        if not reader.read_next_message():
            break