การอ่านไฟล์ Zimbra TGZ
TgzReader บีบอัดและวิเคราะห์สำเนาสำรอง Zimbra ขณะทำงาน ไฟล์ต่อไฟล์ ดังนั้นแม้การส่งออกหลายกิกะไบต์ก็สามารถประมวลผลด้วยหน่วยความจำขนาดเล็ก บทความนี้แสดงวิธีสร้างผู้อ่าน ย้ายผ่านคลังข้อมูลและตรวจสอบตำแหน่งของผู้อ่าน
ตัวอย่างด้านล่างใช้ส่วนหัวและเนมสเปซต่อไปนี้:
#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;
เปิดสำเนาสำรองจากไฟล์
ส่งพาธไปยัง TgzReader คอนสตรัคเตอร์ ผู้อ่านวางตำแหน่งที่รายการแรกทันที ซึ่งเป็นสาเหตุที่ CurrentDirectory สามารถอ่านได้ทันที เรียกใช้ Dispose เพื่อปล่อยไฟล์เมื่อเสร็จ
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();
เปิดสำเนาสำรองจากสตรีม
ใด ๆ ที่อ่านได้ System::IO::Stream ทำงานเป็นแหล่งข้อมูล - ไฟล์ที่เปิดที่อื่น บัฟเฟอร์หน่วยความจำ การดาวน์โหลดจากเครือข่าย เก็บสตรีมเปิดจนกว่าผู้อ่านจะถูกทำลาย
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();
วนซ้ำรายการ
ReadNextMessage ก้าวไปยังรายการต่อไปและคืนค่า false ที่ส่วนท้ายของคลังข้อมูล เนื่องจากผู้อ่านชี้ไปที่รายการแรกหลังการสร้าง do ... while ลูปประมวลผลรายการนั้นก่อนดำเนินต่อ CurrentMessage คือ nullptr สำหรับรายการที่ไม่ใช่ข้อความอีเมล (เช่นรายชื่อติดต่อที่เก็บในสำเนาสำรอง) ดังนั้นตรวจสอบก่อนใช้งาน
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();
อ่านคุณสมบัติข้อความ
รายการปัจจุบันเป็น MailMessage, ดังนั้นหัวเรื่อง ที่อยู่ วันที่ เนื้อหาและไฟล์แนบทั้งหมดจะพร้อมใช้งานขณะวนซ้ำ:
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();
นับรายการ
GetTotalItemsCount คืนค่าจำนวนรายการในสำเนาสำรอง รวมกับตัวนับภายในลูปให้ตัวบ่งชี้ความคืบหน้าแบบ "n จากทั้งหมด" อย่างง่าย:
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();