@@ -19,96 +19,102 @@ For example usage of the library please see the TestGui application stored in th
19
19
20
20
Accessing the contents of a PSTFile is a matter of following the folder structure down to the desired email. This example reads a PST and prints the tree structure to the console:
21
21
22
- package example;
23
- import com.pff.*;
24
- import java.util.*;
25
-
26
- public class Test {
27
- public static void main(String[] args)
28
- {
29
- new Test(args[0]);
22
+ ``` java
23
+ package example ;
24
+ import com.pff.* ;
25
+ import java.util.* ;
26
+
27
+ public class Test {
28
+ public static void main (String [] args )
29
+ {
30
+ new Test (args[0 ]);
31
+ }
32
+
33
+ public Test (String filename ) {
34
+ try {
35
+ PSTFile pstFile = new PSTFile (filename);
36
+ System . out. println(pstFile. getMessageStore(). getDisplayName());
37
+ processFolder(pstFile. getRootFolder());
38
+ } catch (Exception err) {
39
+ err. printStackTrace();
30
40
}
41
+ }
31
42
32
- public Test(String filename) {
33
- try {
34
- PSTFile pstFile = new PSTFile(filename);
35
- System.out.println(pstFile.getMessageStore().getDisplayName());
36
- processFolder(pstFile.getRootFolder());
37
- } catch (Exception err) {
38
- err.printStackTrace();
43
+ int depth = - 1 ;
44
+ public void processFolder (PSTFolder folder )
45
+ throws PSTException , java.io. IOException
46
+ {
47
+ depth++ ;
48
+ // the root folder doesn't have a display name
49
+ if (depth > 0 ) {
50
+ printDepth();
51
+ System . out. println(folder. getDisplayName());
52
+ }
53
+
54
+ // go through the folders...
55
+ if (folder. hasSubfolders()) {
56
+ Vector<PSTFolder > childFolders = folder. getSubFolders();
57
+ for (PSTFolder childFolder : childFolders) {
58
+ processFolder(childFolder);
39
59
}
40
60
}
41
61
42
- int depth = -1;
43
- public void processFolder(PSTFolder folder)
44
- throws PSTException, java.io.IOException
45
- {
62
+ // and now the emails for this folder
63
+ if (folder. getContentCount() > 0 ) {
46
64
depth++ ;
47
- // the root folder doesn't have a display name
48
- if (depth > 0 ) {
65
+ PSTMessage email = ( PSTMessage ) folder. getNextChild();
66
+ while (email != null ) {
49
67
printDepth();
50
- System.out.println(folder.getDisplayName());
51
- }
52
-
53
- // go through the folders...
54
- if (folder.hasSubfolders()) {
55
- Vector<PSTFolder> childFolders = folder.getSubFolders();
56
- for (PSTFolder childFolder : childFolders) {
57
- processFolder(childFolder);
58
- }
59
- }
60
-
61
- // and now the emails for this folder
62
- if (folder.getContentCount() > 0) {
63
- depth++;
64
- PSTMessage email = (PSTMessage)folder.getNextChild();
65
- while (email != null) {
66
- printDepth();
67
- System.out.println("Email: "+email.getSubject());
68
- email = (PSTMessage)folder.getNextChild();
69
- }
70
- depth--;
68
+ System . out. println(" Email: " + email. getSubject());
69
+ email = (PSTMessage )folder. getNextChild();
71
70
}
72
71
depth-- ;
73
72
}
73
+ depth-- ;
74
+ }
74
75
75
- public void printDepth() {
76
- for (int x = 0; x < depth-1; x++) {
77
- System.out.print(" | ");
78
- }
79
- System.out.print(" |- ");
76
+ public void printDepth () {
77
+ for (int x = 0 ; x < depth- 1 ; x++ ) {
78
+ System . out. print(" | " );
80
79
}
80
+ System . out. print(" |- " );
81
81
}
82
+ }
83
+ ```
82
84
83
85
Attachments can be read through PSTAttachment.getFileInputStream like so:
84
86
85
- int numberOfAttachments = email.getNumberOfAttachments();
86
- for (int x = 0; x < numberOfAttachments; x++) {
87
- PSTAttachment attach = email.getAttachment(x);
88
- InputStream attachmentStream = attach.getFileInputStream();
89
- // both long and short filenames can be used for attachments
90
- String filename = attach.getLongFilename();
91
- if (filename.isEmpty()) {
92
- filename = attach.getFilename();
93
- }
94
- FileOutputStream out = new FileOutputStream(filename);
95
- // 8176 is the block size used internally and should give the best performance
96
- int bufferSize = 8176;
97
- byte[] buffer = new byte[bufferSize];
98
- int count = attachmentStream.read(buffer);
99
- while (count == bufferSize) {
100
- out.write(buffer);
101
- count = attachmentStream.read(buffer);
102
- }
103
- byte[] endBuffer = new byte[count];
104
- System.arraycopy(buffer, 0, endBuffer, 0, count);
105
- out.write(endBuffer);
106
- out.close();
107
- attachmentStream.close();
87
+ ``` java
88
+ int numberOfAttachments = email. getNumberOfAttachments();
89
+ for (int x = 0 ; x < numberOfAttachments; x++ ) {
90
+ PSTAttachment attach = email. getAttachment(x);
91
+ InputStream attachmentStream = attach. getFileInputStream();
92
+ // both long and short filenames can be used for attachments
93
+ String filename = attach. getLongFilename();
94
+ if (filename. isEmpty()) {
95
+ filename = attach. getFilename();
96
+ }
97
+ FileOutputStream out = new FileOutputStream (filename);
98
+ // 8176 is the block size used internally and should give the best performance
99
+ int bufferSize = 8176 ;
100
+ byte [] buffer = new byte [bufferSize];
101
+ int count = attachmentStream. read(buffer);
102
+ while (count == bufferSize) {
103
+ out. write(buffer);
104
+ count = attachmentStream. read(buffer);
108
105
}
106
+ byte [] endBuffer = new byte [count];
107
+ System . arraycopy(buffer, 0 , endBuffer, 0 , count);
108
+ out. write(endBuffer);
109
+ out. close();
110
+ attachmentStream. close();
111
+ }
112
+ ```
109
113
110
114
Each object in the PST has a unique identifier called a descriptor node id. This can be useful for retrieving known objects quickly from the PST:
111
115
112
- long id = email.getDescriptorNodeId();
113
- pstObject = PSTObject.detectAndLoadPSTObject(pstFile, id);
116
+ ``` java
117
+ long id = email. getDescriptorNodeId();
118
+ pstObject = PSTObject . detectAndLoadPSTObject(pstFile, id);
119
+ ```
114
120
0 commit comments