Hi All,
I have managed to create and add a (in-memory) zip file as an email attachment. I add a file to the zip file , then push data to this file via a ByteArrayInputStream.
Adding the file (created via ByteArrayInputStream) to an email works perfectly. If i then add another step and zip this file it corrupts the output.
It looks like it is turning the carriage return into a [] . Its 'almost right' as when I cut & paste the contents of the txt file i created (from the zip file) into Word it displays correctly, with the carriage returns. Is it some kind of encoding problem, or mime type issue?
My ByteArrayInputStream uses
buf.append("\n");
for carriage returns. I think the problem is with
ByteArrayInputStream baIn = new ByteArrayInputStream( getAttachementNoFormat(eq_rt.getStoredProc()) );
where I think i need to encode this somehow. Here is the complete code to create and add the zip file to an email. Any isuggestions will be appreciated.
//START of new ZIP code
/* Specify files to be zipped */
// Create temp file.
//File temp = File.createTempFile(fileName, ".txt");
//temp.deleteOnExit();
//BufferedWriter bOut = new BufferedWriter(new FileWriter(temp));
//ByteArrayInputStream baInTemp = new ByteArrayInputStream( getAttachementNoFormat(eq_rt.getStoredProc() ) );
//bOut.write( baInTemp.toString() );
//bOut.close();
String[] filesToZip = new String[3];
filesToZip[0] = "C:\\Program Files\\NetBeans3.6\\firstfile.txt";
filesToZip[1] = "C:\\Program Files\\NetBeans3.6\\secondfile.txt";
filesToZip[2] = "C:\\Program Files\\NetBeans3.6\\thirdfile.txt";
final String fileToZip = fileName;
/* Create a memory buffer to store the ByteArray, fixed size */
byte[] buffer = new byte[18024];
/* Specify zip file name */
String zipFileName= eq_rt.getReportName() + ".zip";
try {
/* Create ZipOutputStream to store the FileOutputStream */
// ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
ZipOutputStream out = new ZipOutputStream(byteArray);
/* Set the compression ratio */
out.setLevel(Deflater.DEFAULT_COMPRESSION);
/* iterate through the array of files, adding each to the zip file */
for (int a = 0; a < filesToZip.length; a++) {
/* Print the filenumber being added to the zip */
System.out.println(a);
/* Associate a file input stream for the current file */
// FileInputStream in = new FileInputStream(filesToZip[a]);
/* *************************
This ROCKS as it is passing a array into the text file .getBytes() seems
to be the KEY in getting ByteArrayInputStream to WORK
************************* */
//String strSocketInput = "TAIWAN";
//ByteArrayInputStream baIn = new ByteArrayInputStream(strSocketInput.getBytes());
//String strSocketInput = new String (getAttachementNoFormat(eq_rt.getStoredProc()).toString() );
//ByteArrayInputStream baIn = new ByteArrayInputStream( getAttachementNoFormat(eq_rt.getStoredProc()) );
ByteArrayInputStream baIn = new ByteArrayInputStream( getAttachementNoFormat(eq_rt.getStoredProc()) );
//String strSocketInput = "TAIWAN";
//ByteArrayInputStream baIn = new ByteArrayInputStream(strSocketInput.getBytes());
/* Add ZIP entry to output stream. */
out.putNextEntry(new ZipEntry(filesToZip[a]));
/* Transfer bytes from the current file to the ZIP file */
int len;
while ((len = baIn.read(buffer)) > 0)
{
out.write(buffer, 0, len);
}
/* Close the current entry */
out.closeEntry();
/* Close the current file input stream */
baIn.close();
}
/* Close the ZipOutPutStream (very important to close the zip before you attach it to the email) Thanks DrClap */
out.close();
/* Create a datasource for email attachment */
// DataSource sourcezip = new FileDataSource(zipFileName);
DataSource sourcezip = new ByteArrayDataSource(byteArray.toByteArray(), zipFileName, "application/gzip" );
/* Create a new MIME bodypart */
BodyPart attachment = new MimeBodyPart();
attachment.setDataHandler(new DataHandler(sourcezip));
attachment.setFileName(zipFileName);
/* attach the attachemnts to the mail */
multipart.addBodyPart(attachment);
}
catch (IllegalArgumentException iae) {
iae.printStackTrace();
}
catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
// End Of New ZIP code