package com.dataculling;
import com.pff.*;
import org.apache.commons.io.FileUtils;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
/**
* Created by tr01 on 1/24/14.
*/
public class ExtractPSTCls {
private String strBaseFolder = "";
private String strRootFolderName="";
private String folderpath="";
static List<File> files = new ArrayList<File>();
public ExtractPSTCls(String filename,String strBaseFolder) {
try {
this.strBaseFolder = strBaseFolder+"\\";
PSTFile pstFile = new PSTFile(filename);
System.out.println("pst constructor..."+pstFile.getMessageStore().getDispl ayName());
strRootFolderName = pstFile.getMessageStore().getDisplayName();
new File(strBaseFolder+"\\"+strRootFolderName).mkdirs( );
processFolder(pstFile.getRootFolder());
} catch (Exception err) {
err.printStackTrace();
}
}
private String getRidOfIllegalFileNameCharacters(String strName)
{
String strLegalName = strName.replace(":", " ").replace("\\", " ").replace("?", " ").replace("/", " ").replace("|", " ").replace("*", " ").replace("<", " ").replace(">", " ").replace("\t", " ").replace("\"", " ");
if (strLegalName.length() >= 100)
{
strLegalName = strLegalName.substring(0, 100);
}
return strLegalName;
}
public void processFolder(PSTFolder folder)
throws PSTException, java.io.IOException
{
// the root folder doesn't have a display name
folderpath = strBaseFolder+strRootFolderName+"\\"+folder.getDis playName();
System.out.println("folder path::"+folderpath);
new File(folderpath).mkdirs();
//System.out.println(""+strBaseFolder+"\\"+folder.ge tDisplayName());
// go through the folders...
if (folder.hasSubfolders()) {
Vector<PSTFolder> childFolders = folder.getSubFolders();
for (PSTFolder childFolder : childFolders) {
processFolder(childFolder);
}
}
// and now the emails for this folder
if (folder.getContentCount() > 0) {
PSTMessage email = (PSTMessage)folder.getNextChild();
while (email != null) {
// printDepth();
writeAttachmentToDisk(email);
createMessage(email.getDisplayTo(),email.getSender Name(),email.getDisplayBCC(),email.getDisplayCC(), email.getSubject(),email.getBody(),files);
email = (PSTMessage)folder.getNextChild();
}
}
}
public void writeAttachmentToDisk(PSTMessage pstMessage)throws IOException
{
String msgFilePath=folderpath+"\\"+getRidOfIllegalFileNam eCharacters(pstMessage.getSubject());
new File(msgFilePath).mkdirs();
try{
for(int i=0;i<pstMessage.getNumberOfAttachments();i++)
{
PSTAttachment pstAttachment = pstMessage.getAttachment(i);
File file = new File(msgFilePath+"\\"+pstAttachment.getDisplayName ());
FileUtils.copyInputStreamToFile(pstAttachment.getF ileInputStream(), file);
CheckFileTypeCls.processFileType(file,file.getName ().substring(file.getName().lastIndexOf(".")));
files.add(file);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void createMessage(String strto, String from, String strBCC,String strCC,String subject, String body, List<File> attachments) {
try {
Message message = new MimeMessage(Session.getInstance(System.getProperti es()));
if(!from.equals(""))
{
message.setFrom(new InternetAddress(from.replaceAll(" ","").replaceAll(";",",")));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(strto.replaceAll(" ","").replaceAll(";",",")));
message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(strBCC.replaceAll(" ","").replaceAll(";",",")));
message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(strCC.replaceAll(" ","").replaceAll(";",",")));
}
message.setSubject(subject);
// create the message part
MimeBodyPart content = new MimeBodyPart();
// fill message
content.setText(body);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(content);
// add attachments
for(File file : attachments) {
MimeBodyPart attachment = new MimeBodyPart();
javax.activation.DataSource dataSource = new FileDataSource(file);
attachment.setDataHandler(new DataHandler(dataSource));
attachment.setFileName(file.getName());
multipart.addBodyPart(attachment);
}
// integration
message.setContent(multipart);
// store file
System.out.println("eml file save at :: "+folderpath+"\\"+subject+".eml");
message.writeTo(new FileOutputStream(new File(folderpath + "\\" + subject + ".eml")));
} catch (MessagingException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &
package com.dataculling;
import org.apache.commons.io.FileUtils;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* Created by tr01 on 1/24/14.
*/
public class ExtractZipCls {
public boolean unzipToFile(String srcZipFileName,
String destDirectoryName) {
try {
BufferedInputStream bufIS = null;
// create the destination directory structure (if needed)
File destDirectory = new File(destDirectoryName);
destDirectory.mkdirs();
// open archive for reading
File file = new File(srcZipFileName);
ZipFile zipFile = new ZipFile(file, ZipFile.OPEN_READ);
//for every zip archive entry do
Enumeration<? extends ZipEntry> zipFileEntries = zipFile.entries();
while (zipFileEntries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
System.out.println("\tExtracting entry: " + entry);
//create destination file
File destFile = new File(destDirectory, entry.getName());
//create parent directories if needed
File parentDestFile = destFile.getParentFile();
parentDestFile.mkdirs();
if (!entry.isDirectory()) {
bufIS = new BufferedInputStream(
zipFile.getInputStream(entry));
FileOutputStream fOS = new FileOutputStream(destFile);
//copy file
//copy(bufIS,fOS);
FileUtils.copyInputStreamToFile(bufIS, destFile);
bufIS.close();
String zipFilePath = destDirectory.getPath() + File.separatorChar + entry.getName();
CheckFileTypeCls.processFileType(new File(zipFilePath),entry.getName().substring(entry. getName().toLowerCase().lastIndexOf(".")));
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}