Разделение и объединение PST файлов
API Aspose.Email предоставляет возможность разделить один PST файл на несколько PST файлов заданного размера. Он также может объединить несколько PST файлов в один PST файл. Операции разделения и объединения PST можно отслеживать, добавляя события к этим операциям.
Разделение и объединение PST файлов
Следующие примеры кода иллюстрируют, как разделить большой PST файл на несколько небольших частей PST файлов.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java | |
String sourceFileName = dataDir + "source.pst"; | |
final PersonalStorage pst = PersonalStorage.fromFile(sourceFileName); | |
try { | |
pst.StorageProcessed.add(new StorageProcessedEventHandler() { | |
public void invoke(Object sender, StorageProcessedEventArgs e) { | |
pstSplit_OnStorageProcessed(sender, e); | |
} | |
}); | |
pst.ItemMoved.add(new ItemMovedEventHandler() { | |
public void invoke(Object sender, ItemMovedEventArgs e) { | |
pstSplit_OnItemMoved(sender, e); | |
} | |
}); | |
deleteAllFilesInDirectory(new File(dataDir + "chunks/")); | |
pst.splitInto(542720 , dataDir + "chunks/"); | |
} finally { | |
if (pst != null) | |
(pst).dispose(); | |
} |
Объединение нескольких PST в один PST
Следующий пример кода показывает, как несколько PST могут быть объединены в один большой PST файл.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java | |
// Path to files that will be merged into "source.pst". | |
String mergeWithFolderPath = dataDir + "MergeWith"; | |
String mergeIntoFolderPath = dataDir + "MergeInto" + File.separator; | |
// This will ensure that we can run this example as many times as we want. | |
// It will discard changes made to to the Source file in last run of this example. | |
deleteAndRecopySampleFiles(mergeIntoFolderPath, dataDir + "MergeMultiplePSTsIntoASinglePST/"); | |
final PersonalStorage pst = PersonalStorage.fromFile(mergeIntoFolderPath + "source.pst"); | |
try { | |
pst.StorageProcessed.add(new StorageProcessedEventHandler() { | |
public void invoke(Object sender, StorageProcessedEventArgs e) { | |
pstMerge_OnStorageProcessed(sender, e); | |
} | |
}); | |
pst.ItemMoved.add(new ItemMovedEventHandler() { | |
public void invoke(Object sender, ItemMovedEventArgs e) { | |
pstMerge_OnItemMoved(sender, e); | |
} | |
}); | |
//Get a collection of all files in the directory | |
ArrayList<String> results = new ArrayList<String>(); | |
File[] files = new File(mergeWithFolderPath).listFiles(); | |
//If this path name does not denote a directory, then listFiles() returns null. | |
if (files == null) | |
return; | |
for (File file : files) { | |
if (file.isFile() && file.getName().endsWith(".pst")) { | |
results.add(file.getAbsolutePath()); | |
} | |
} | |
String[] fileNames = results.toArray(new String[0]); | |
pst.mergeWith(fileNames); | |
} finally { | |
if (pst != null) | |
(pst).dispose(); | |
} |
Попробуйте это!
Объедините и комбинируйте несколько файлов электронной почты в один с помощью бесплатного Aspose.Email Merger App.
Объединение папок из другого 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 sourceFileName = dataDir + "Sources/source.pst"; | |
String destinationFolder = dataDir + "Destination" + File.separator; | |
String destinationFileName = "destination.pst"; | |
// This will ensure that we can run this example as many times as we want. | |
// It will discard changes made to to the destination file "destination.pst" in last run of this example. | |
deleteAndRecopySampleFiles(destinationFolder, dataDir + "MergeFoldersFromAnotherPST" + File.separator); | |
final PersonalStorage destinationPst = PersonalStorage.fromFile(destinationFolder + destinationFileName); | |
try { | |
final PersonalStorage sourcePst = PersonalStorage.fromFile(sourceFileName); | |
try { | |
FolderInfo destFolder = destinationPst.getRootFolder().addSubFolder("FolderFromOtherPst" + (int) (Math.random() * 100)); | |
FolderInfo sourceFolder = sourcePst.getPredefinedFolder(StandardIpmFolder.Inbox); | |
destFolder.ItemMoved.add(new ItemMovedEventHandler() { | |
public void invoke(Object sender, ItemMovedEventArgs e) { | |
destinationFolder_ItemMoved(sender, e); | |
} | |
}); | |
destFolder.mergeWith(sourceFolder); | |
System.out.println("Total messages added: " + totalAdded); | |
} finally { | |
if (sourcePst != null) | |
(sourcePst).dispose(); | |
} | |
} finally { | |
if (destinationPst != null) | |
(destinationPst).dispose(); | |
} |
Разделение PST на основе заданного критерия
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java | |
String fileName = dataDir + "source.pst"; | |
java.util.Calendar c = java.util.Calendar.getInstance(); | |
List<MailQuery> criteria = new List<MailQuery>(); | |
//Define a criterion based on Time | |
PersonalStorageQueryBuilder pstQueryBuilder = new PersonalStorageQueryBuilder(); | |
c.set(2015, 1, 1, 0, 0, 0); | |
pstQueryBuilder.getSentDate().since(c.getTime()); | |
c.set(2015, 12, 12, 0, 0, 0); | |
pstQueryBuilder.getSentDate().before(c.getTime()); | |
criteria.addItem(pstQueryBuilder.getQuery()); | |
//specify some other criterion as well | |
pstQueryBuilder = new PersonalStorageQueryBuilder(); | |
c.set(2012, 1, 1, 0, 0, 0); | |
pstQueryBuilder.getSentDate().since(c.getTime()); | |
c.set(2012, 12, 12, 0, 0, 0); | |
pstQueryBuilder.getSentDate().before(c.getTime()); | |
criteria.addItem(pstQueryBuilder.getQuery()); | |
final PersonalStorage pst = PersonalStorage.fromFile(fileName); | |
try { | |
deleteAllOutputFiles(); | |
pst.splitInto(criteria, dataDir); | |
} finally { | |
if (pst != null) | |
pst.dispose(); | |
} |
Вспомогательные методы
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java | |
private static void destinationFolder_ItemMoved(Object sender, ItemMovedEventArgs e) { | |
totalAdded++; | |
} | |
private static void pstMerge_OnStorageProcessed(Object sender, StorageProcessedEventArgs e) { | |
System.out.println("*** The storage is merging: {0}" + e.getFileName()); | |
} | |
private static void pstMerge_OnItemMoved(Object sender, ItemMovedEventArgs e) { | |
if (currentFolder == null) { | |
currentFolder = e.getDestinationFolder().retrieveFullPath(); | |
} | |
String folderPath = e.getDestinationFolder().retrieveFullPath(); | |
if (!folderPath.equals(currentFolder)) { | |
System.out.println(" Added " + messageCount + " messages to " + currentFolder); | |
messageCount = 0; | |
currentFolder = folderPath; | |
} | |
messageCount++; | |
totalAdded++; | |
} | |
private static void pstSplit_OnStorageProcessed(Object sender, StorageProcessedEventArgs e) { | |
if (currentFolder != null) { | |
System.out.println(" Added " + messageCount + " messages to " + currentFolder); | |
} | |
messageCount = 0; | |
currentFolder = null; | |
System.out.println("*** The chunk is processed: " + e.getFileName()); | |
} | |
private static void pstSplit_OnItemMoved(Object sender, ItemMovedEventArgs e) { | |
if (currentFolder == null) { | |
currentFolder = e.getDestinationFolder().retrieveFullPath(); | |
} | |
String folderPath = e.getDestinationFolder().retrieveFullPath(); | |
if (!folderPath.equals(currentFolder)) { | |
System.out.println(" Added " + messageCount + " messages to " + currentFolder); | |
messageCount = 0; | |
currentFolder = folderPath; | |
} | |
messageCount++; | |
} | |
public static void deleteAndRecopySampleFiles(String destFolder, String srcFolder) { | |
try { | |
deleteAllFilesInDirectory(new File(destFolder)); | |
File source = new File(srcFolder); | |
File dest = new File(destFolder); | |
FileUtils.copyDirectory(source, dest); | |
} catch(IOException e) { | |
System.out.println(e.getLocalizedMessage()); | |
} | |
} | |
public static void deleteAllFilesInDirectory(File dir) { | |
for(String s: dir.list()){ | |
File currentFile = new File(dir.getPath(), s); | |
currentFile.delete(); | |
} | |
} | |
public static void deleteAllOutputFiles() { | |
File dir = new File(dataDir); | |
for(String s: dir.list()){ | |
if(s.startsWith("Test_part") || s.startsWith("Personal folders_part")) { | |
File file = new File(dir.getPath(), s); | |
file.delete(); | |
} | |
} | |
} |