Splitting and Merging PST Files

Aspose.Email API provides the capability to split a single PST file into multiple PST files of desired file size. It can also merge multiple PST files into a single PST file. Both the splitting and merging of PSTs operations can be tracked by adding events to these operations.

Split and Merge PST Files

The following code samples illustrate how to split a large PST file into multiple small chunks of PST files.

// 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();
}

Merging of Multiple PSTs into a Single PST

The following code sample shows how multiple PSTs can be merged into a single large PST file.

// 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();
}

Merge Folders from another PST

Aspose.Email API allows merging complete folders from another PST to a destination PST, thus resulting in a large size 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();
}

Split PST based on Defined Criterion

// 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();
}

Tracking the Progress of a Split or a Merge

Both operations raise events while they run, and the samples above attach a handler to each of them:

A handler is an anonymous StorageProcessedEventHandler or ItemMovedEventHandler added with add(). To count the messages moved, keep the counter in a field or in a one-element array, as the merge sample does, because a local variable captured by an anonymous class has to be effectively final.