-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathPstExport.java
More file actions
275 lines (249 loc) · 6.92 KB
/
PstExport.java
File metadata and controls
275 lines (249 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package example;
import com.pff.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.*;
import org.json.simple.*;
@SuppressWarnings("unchecked")
public class PstExport {
public static void main(String[] args)
{
if (args.length < 2)
{
System.out.println("Usage: PstExport Src_File Dst_Folder");
}
else
new PstExport(args[0], args[1]);
}
public PstExport(String filename, String destfolder) {
try {
PSTFile pstFile = new PSTFile(filename);
System.out.println(pstFile.getMessageStore().getDisplayName());
processFolder(pstFile.getRootFolder(), new java.io.File(destfolder));
} catch (Exception err) {
err.printStackTrace();
}
}
int depth = -1;
int msgs = -1;
Random rnd = new Random();
public JSONObject processFolder(PSTFolder folder, File destfolder)
throws PSTException, java.io.IOException
{
depth++;
// the root folder doesn't have a display name
JSONObject json = new JSONObject();
json.put("Id", folder.getDescriptorNodeId());
JSONObject jsonfld = new JSONObject();
JSONObject jsonmsg = new JSONObject();
if (depth > 0) {
printDepth();
System.out.println(folder.getDisplayName());
json.put("Name", folder.getDisplayName());
}
else
{
json.put("Name","[root]");
}
// go through the folders...
if (folder.hasSubfolders()) {
Vector<PSTFolder> childFolders = folder.getSubFolders();
for (PSTFolder childFolder : childFolders) {
String subf="F"+ Long.toString(childFolder.getDescriptorNodeId());
JSONObject fld = processFolder(childFolder, new File(destfolder, subf));
jsonfld.put(subf, fld);
}
}
// and now the emails for this folder
if (folder.getContentCount() > 0) {
depth++;
PSTMessage email = (PSTMessage)folder.getNextChild();
while (email != null) {
printDepth();
System.out.println("Email: "+email.getSubject());
if (msgs<0 || (rnd.nextInt(10)==1 && email.getSubject().compareTo("")!=0 && msgs!=0))
{
File subf = new File(destfolder, "XX");
String msgn = "M"+email.getDescriptorNodeId();
Date dtime = email.getMessageDeliveryTime();
if (dtime != null)
{
String y = new SimpleDateFormat("yyyy").format(dtime);
String m = new SimpleDateFormat("MM").format(dtime);
subf = new File(destfolder, y);
subf = new File(subf, m);
msgn = y+"-"+m+"-"+msgn;
}
else
{
msgn = "XX-" + msgn;
}
subf = new File(subf, msgn);
JSONObject msg = processEmail(email, subf);
jsonmsg.put(msgn, msg);
msgs--;
}
email = (PSTMessage)folder.getNextChild();
}
depth--;
}
if (jsonfld.size()>0)
json.put("Folders", jsonfld);
if (jsonmsg.size()>0)
json.put("Messages", jsonmsg);
destfolder.mkdirs();
FileWriter fw = new FileWriter(new File(destfolder,"index.json"));
json.writeJSONString(fw);
fw.flush();
fw.close();
depth--;
return json;
}
public JSONObject processEmail(PSTMessage email, File destfolder)
throws PSTException, IOException, FileNotFoundException
{
JSONObject json = new JSONObject();
json.put("Subject", email.getSubject());
json.put("Id", email.getDescriptorNodeId());
Date dtime = email.getMessageDeliveryTime();
if (dtime != null)
{
json.put("DeliveryTime",new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(dtime));
}
json.put("SenderAddress", email.getSenderEmailAddress());
json.put("Sender", email.getSenderName());
destfolder.mkdirs();
JSONObject jsonrec = new JSONObject();
int numberOfRecipients = email.getNumberOfRecipients();
for (int x = 0; x < numberOfRecipients; x++)
{
JSONObject rec = new JSONObject();
PSTRecipient recipient;
try
{
recipient = email.getRecipient(x);
}
catch (java.lang.IndexOutOfBoundsException e)
{
recipient = null;
}
if (recipient != null)
{
rec.put("Name", recipient.getDisplayName());
rec.put("Address", recipient.getEmailAddress());
switch (recipient.getRecipientType())
{
case PSTRecipient.MAPI_TO:
rec.put("Type", "To");
break;
case PSTRecipient.MAPI_CC:
rec.put("Type", "CC");
break;
case PSTRecipient.MAPI_BCC:
rec.put("Type", "BCC");
break;
default:
break;
}
jsonrec.put(x, rec);
}
}
JSONObject jsonatt = new JSONObject();
int numberOfAttachments = email.getNumberOfAttachments();
for (int x = 0; x < numberOfAttachments; x++)
{
JSONObject att = new JSONObject();
PSTAttachment attach = email.getAttachment(x);
att.put("Id",x);
jsonatt.put(x, att);
InputStream attachmentStream = attach.getFileInputStream();
// both long and short filenames can be used for attachments
String filename = attach.getLongFilename();
if (filename.isEmpty()) {
filename = attach.getFilename();
}
att.put("Name", filename);
att.put("Size", attach.getAttachSize());
FileOutputStream out;
try
{
out = new FileOutputStream(new File(destfolder,filename));
}
catch (FileNotFoundException e)
{
String[] tok = filename.split("\\.(?=[^\\.]+$)");
if (tok.length > 1)
{
filename = String.valueOf(x) + "." + tok[1];
}
else
{
filename = String.valueOf(x);
}
out = new FileOutputStream(new File(destfolder,filename));
att.put("SavedAs", filename);
}
// 8176 is the block size used internally and should give the best performance
int bufferSize = 8176;
byte[] buffer = new byte[bufferSize];
int count = attachmentStream.read(buffer);
while (count == bufferSize) {
out.write(buffer);
count = attachmentStream.read(buffer);
}
byte[] endBuffer = new byte[count];
System.arraycopy(buffer, 0, endBuffer, 0, count);
out.write(endBuffer);
out.close();
attachmentStream.close();
}
FileWriter fw;
String html = email.getBodyHTML();
if (html.compareTo("") != 0)
{
json.put("HTML", "Message.html");
fw = new FileWriter(new File(destfolder,"Message.html"));
fw.write(html);
fw.close();
}
String txt = email.getBody();
if (txt.compareTo("") != 0)
{
json.put("txt", "Message.txt");
fw = new FileWriter(new File(destfolder,"Message.txt"));
fw.write(txt);
fw.close();
}
String rtf = email.getRTFBody();
if (rtf.compareTo("") != 0)
{
json.put("rtf", "Message.rtf");
fw = new FileWriter(new File(destfolder,"Message.rtf"));
fw.write(rtf);
fw.close();
}
String headers = email.getTransportMessageHeaders();
if (headers.compareTo("") != 0)
{
json.put("Headers", "Headers.txt");
fw = new FileWriter(new File(destfolder,"Headers.txt"));
fw.write(headers);
fw.close();
}
if (jsonatt.size()>0)
json.put("Attachments", jsonatt);
if (jsonrec.size()>0)
json.put("Recipients", jsonrec);
fw = new FileWriter(new File(destfolder,"index.json"));
json.writeJSONString(fw);
fw.flush();
fw.close();
return json;
}
public void printDepth() {
for (int x = 0; x < depth-1; x++) {
System.out.print(" | ");
}
System.out.print(" |- ");
}
}