손상된 PST/OST 파일 읽기
손상된 PST/OST 파일 읽기
때때로 일부 문제 때문에 PST/OST를 읽을 수 없을 수 있습니다. 예를 들어, 일부 데이터 블록이 손상될 수 있습니다. 이러한 경우, 일반적으로 해당 메서드를 호출할 때 예외가 발생합니다. EnumerateFolders, EnumerateMessages, GetContents, GetSubfolders, 등 메서드. 그러나 개별 메시지나 폴더는 저장소에서 손상되지 않을 수 있습니다.
Aspose.Email 사용자는 계층적으로 항목 식별자를 찾을 수 있습니다. 또한 식별자를 통해 항목을 추출할 수 있습니다. 이를 위해 라이브러리는 다음 메서드들을 제공합니다:
- PersonalStorage.findMessages(String parentEntryId) - 폴더의 메시지 식별자를 찾습니다.
- PersonalStorage.findSubfolders(String parentEntryId) - 폴더의 하위 폴더 식별자를 찾습니다.
Note, 그럼에도 불구하고 장점이 있더라도 이러한 메서드로도 읽을 수 없는 손상된 저장소가 있습니다.
다음 코드 스니펫은 손상된 PST/OST 파일을 읽는 데 이러한 메서드들을 사용하는 방법을 보여줍니다:
try (PersonalStorage pst = PersonalStorage.fromFile(fileName)) {
exploreCorruptedPst(pst, pst.getRootFolder().getEntryIdString());
}
public static void exploreCorruptedPst(PersonalStorage pst, String rootFolderId) {
Iterable<String> messageIdList = pst.findMessages(rootFolderId);
for (String messageId : messageIdList) {
try {
MapiMessage msg = pst.extractMessage(messageId);
System.out.println("- " + msg.getSubject());
} catch (Exception e) {
System.out.println("Message reading error. Entry id: " + messageId);
}
}
Iterable<String> folderIdList = pst.findSubfolders(rootFolderId);
for (String subFolderId : folderIdList) {
if (subFolderId != rootFolderId) {
try {
FolderInfo subfolder = pst.getFolderById(subFolderId);
System.out.println(subfolder.getDisplayName());
} catch (Exception e) {
System.out.println("Message reading error. Entry id: " + subFolderId);
}
exploreCorruptedPst(pst, subFolderId);
}
}
}
손상된 파일에서 PST 항목 추출
탐색 API는 원본 파일의 일부 데이터가 손상된 경우에도 예외를 발생시키지 않고 가능한 한 모든 PST 항목을 추출할 수 있게 합니다.
다음 사용 PersonalStorage(TraversalExceptionsCallback callback) 생성자와 load(String fileName) 메서드 대신에 fromFile 메서드.
생성자를 통해 콜백 메서드를 정의할 수 있습니다.
TraversalExceptionsCallback exceptionsCallback = new TraversalExceptionsCallback() {
@Override
public void invoke(TraversalAsposeException exception, String itemId) {
/* Exception handling code. */
}
};
try (PersonalStorage pst = new PersonalStorage(exceptionsCallback)) { }
로드 및 탐색 중 발생한 예외는 콜백 메서드를 통해 확인할 수 있습니다.
load 메서드는 파일이 성공적으로 로드되고 추가 탐색이 가능한 경우 ’true’를 반환합니다. 파일이 손상되어 탐색이 불가능한 경우 ‘false’가 반환됩니다.
if (currentPst.load(inputStream))
다음 코드 샘플은 프로젝트에 PST 파일 탐색 API를 구현하는 방법을 보여줍니다:
public static void main(String[] args) {
TraversalExceptionsCallback exceptionsCallback = new TraversalExceptionsCallback() {
@Override
public void invoke(TraversalAsposeException exception, String itemId) {
/* Exception handling code. */
}
};
try (PersonalStorage pst = new PersonalStorage(exceptionsCallback)) {
if (pst.load("test.pst")) {
getAllMessages(pst, pst.getRootFolder());
}
}
}
private static void getAllMessages(PersonalStorage pst, FolderInfo folder) {
for (String messageEntryId : folder.enumerateMessagesEntryId()) {
MapiMessage message = pst.extractMessage(messageEntryId);
}
for (FolderInfo subFolder : folder.getSubFolders()) {
getAllMessages(pst, subFolder);
}
}