Đọc tệp PST/OST bị hỏng

Đọc tệp PST/OST bị hỏng

Đôi khi việc đọc PST/OST có thể không khả thi do một số vấn đề. Ví dụ, một số khối dữ liệu có thể bị hỏng. Trong những trường hợp như vậy, ngoại lệ thường xảy ra khi gọi EnumerateFolders, EnumerateMessages, GetContents, GetSubfolders, v.v. các phương thức. Tuy nhiên, các tin nhắn hoặc thư mục riêng lẻ có thể vẫn không bị hỏng trong kho lưu trữ.

Người dùng Aspose.Email có thể tìm các định danh mục theo chiều phân cấp. Tiếp theo, bạn có thể trích xuất các mục bằng định danh. Để đạt mục đích này, thư viện cung cấp các phương thức sau:

Lưu ý, rằng dù có những ưu điểm, vẫn có các kho lưu trữ bị hỏng không thể đọc được ngay cả khi sử dụng các phương thức này.

Đoạn mã dưới đây minh họa cách sử dụng các phương thức này để đọc các tệp PST/OST bị hỏng:

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

Trích xuất mục PST từ các tệp bị hỏng

API duyệt cho phép trích xuất càng nhiều mục PST càng tốt, mà không ném ra ngoại lệ, ngay cả khi một số dữ liệu của tệp gốc bị hỏng.

Sử dụng PersonalStorage(TraversalExceptionsCallback callback) hàm khởi tạo và load(String fileName) phương thức thay vì fromFile phương thức.

Constructor cho phép định nghĩa một phương thức callback.

TraversalExceptionsCallback exceptionsCallback = new TraversalExceptionsCallback() {
    @Override
    public void invoke(TraversalAsposeException exception, String itemId) {
        /* Exception handling  code. */
    }
};

try (PersonalStorage pst = new PersonalStorage(exceptionsCallback)) { }

Các ngoại lệ khi tải và duyệt sẽ được cung cấp thông qua phương thức callback.

Phương thức load trả về ’true’ nếu tệp đã được tải thành công và có thể tiếp tục duyệt. Nếu tệp bị hỏng và không thể duyệt, sẽ trả về ‘false’.

if (currentPst.load(inputStream))

Đoạn mã mẫu sau cho thấy cách triển khai API duyệt tệp PST vào một dự án:

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