פיצול ומיזוג קבצי PST

API של Aspose.Email מספק יכולת לפצל קובץ PST יחיד למספר קבצי PST בגודל רצוי. הוא יכול גם למזג מספר קבצי PST לקובץ PST אחד. ניתן לעקוב אחרי פעולות הפיצול והמיזוג של PSTs על ידי הוספת אירועים לפעולות אלו.

פצל ומזג קבצי PST

דוגמאות הקוד הבאות ממחישות כיצד לפצל קובץ PST גדול לחלקים קטנים מרובים של קבצי PST.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

PersonalStorage pst = PersonalStorage.fromFile(dataDir + "source.pst");
try {
    pst.StorageProcessed.add(new StorageProcessedEventHandler() {
        public void invoke(Object sender, StorageProcessedEventArgs e) {
            System.out.println("*** The chunk is processed: " + e.getFileName());
        }
    });

    pst.ItemMoved.add(new ItemMovedEventHandler() {
        public void invoke(Object sender, ItemMovedEventArgs e) {
            System.out.println("Moved to " + e.getDestinationFolder().retrieveFullPath());
        }
    });

    // Split the source PST into chunks of roughly 530 KB each
    pst.splitInto(542720, dataDir + "chunks/");
} finally {
    pst.dispose();
}

מיזוג מספר PSTs לקובץ PST אחד

דוגמת הקוד הבאה מראה כיצד ניתן למזג מספר קבצי PST לקובץ PST גדול יחיד.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

// The PST files that will be merged into source.pst
String mergeWithFolderPath = dataDir + "MergeWith";

PersonalStorage pst = PersonalStorage.fromFile(dataDir + "source.pst");
try {
    pst.StorageProcessed.add(new StorageProcessedEventHandler() {
        public void invoke(Object sender, StorageProcessedEventArgs e) {
            System.out.println("*** The storage is merging: " + e.getFileName());
        }
    });

    pst.ItemMoved.add(new ItemMovedEventHandler() {
        public void invoke(Object sender, ItemMovedEventArgs e) {
            System.out.println("Moved to " + e.getDestinationFolder().retrieveFullPath());
        }
    });

    // Collect every PST file in the directory
    List<String> results = new ArrayList<String>();
    File[] files = new File(mergeWithFolderPath).listFiles();

    // listFiles() returns null if the path does not denote a directory
    if (files != null) {
        for (File file : files) {
            if (file.isFile() && file.getName().endsWith(".pst")) {
                results.add(file.getAbsolutePath());
            }
        }

        pst.mergeWith(results.toArray(new String[0]));
    }
} finally {
    pst.dispose();
}

מזג תיקיות מקובץ PST אחר

API של Aspose.Email מאפשר מיזוג תיקיות מלאות מקובץ PST אחר ל‑PST יעד, כך שנוצר PST בגודל גדול.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

PersonalStorage destinationPst = PersonalStorage.fromFile(dataDir + "destination.pst");
try {
    PersonalStorage sourcePst = PersonalStorage.fromFile(dataDir + "source.pst");
    try {
        FolderInfo destFolder = destinationPst.getRootFolder().addSubFolder("FolderFromOtherPst");
        FolderInfo sourceFolder = sourcePst.getPredefinedFolder(StandardIpmFolder.Inbox);

        final int[] totalAdded = { 0 };
        destFolder.ItemMoved.add(new ItemMovedEventHandler() {
            public void invoke(Object sender, ItemMovedEventArgs e) {
                totalAdded[0]++;
            }
        });

        destFolder.mergeWith(sourceFolder);

        System.out.println("Total messages added: " + totalAdded[0]);
    } finally {
        sourcePst.dispose();
    }
} finally {
    destinationPst.dispose();
}

פצל PST בהתבסס על קריטריון מוגדר

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

java.util.Calendar c = java.util.Calendar.getInstance();

// splitInto takes the library's own generic list, not java.util.List
com.aspose.email.system.collections.generic.List<MailQuery> criteria =
        new com.aspose.email.system.collections.generic.List<MailQuery>();

// Define a criterion based on the sent date
PersonalStorageQueryBuilder pstQueryBuilder = new PersonalStorageQueryBuilder();
c.set(2015, java.util.Calendar.JANUARY, 1, 0, 0, 0);
pstQueryBuilder.getSentDate().since(c.getTime());
c.set(2015, java.util.Calendar.DECEMBER, 12, 0, 0, 0);
pstQueryBuilder.getSentDate().before(c.getTime());
criteria.addItem(pstQueryBuilder.getQuery());

// Specify another criterion as well
pstQueryBuilder = new PersonalStorageQueryBuilder();
c.set(2012, java.util.Calendar.JANUARY, 1, 0, 0, 0);
pstQueryBuilder.getSentDate().since(c.getTime());
c.set(2012, java.util.Calendar.DECEMBER, 12, 0, 0, 0);
pstQueryBuilder.getSentDate().before(c.getTime());
criteria.addItem(pstQueryBuilder.getQuery());

PersonalStorage pst = PersonalStorage.fromFile(dataDir + "source.pst");
try {
    pst.splitInto(criteria, dataDir);
} finally {
    pst.dispose();
}

מעקב אחר ההתקדמות של פיצול או מיזוג

שתי הפעולות מעלות אירועים בזמן הרצה, והדוגמאות למעלה מצרפות מטפל לכל אחד מהם:

מטפל הוא אנונימי StorageProcessedEventHandler או ItemMovedEventHandler התווסף עם add(). כדי לספור את ההודעות שהועברו, שמור את המונה בשדה או במערך בעל אלמנט אחד, כפי שהדוגמה של המיזוג עושה, מכיוון שמשתנה מקומי שנתפס על‑ידי מחלקה אנונימית צריך להיות אפקטיבית קבוע.