I am trying to extract a zip file attachment from an email with Java.
The project is using Aspose email.
I would like to know if I use attachment.getContentStream() to get the inputStream, would I be able to treat the inputStream as a Java File Type and convert it a Zipfile type and extract the content from the zip file.
Please accept my apologies for not getting back to you soon.
Aspose.Email saves attachments to messages by reading these as array of bytes from the disc and if you extract the content stream, it returns you the proper stream of the attachment, be it of any type. In this case, I have tested the issue with the following sample code and it printed out the names of the inner files in the zip archive properly. I am sorry but I haven’t worked with Apache commons to explore it further, but In my opinion it should work properly in this regard. Please let us know if we can be of any additional help in this regard.
Java Code:
//Message having some zip file as attachment
MailMessage msg = MailMessage.load("Test Zip Attachment.msg");
//Get the only attachment from the message file
Attachment att = (Attachment) msg.getAttachments().get(0);
//Define ZipArchiveInputStream
ZipArchiveInputStream zin = null;
try {
zin = new ZipArchiveInputStream(att.getContentStream());
ZipArchiveEntry zae = zin.getNextZipEntry();
System.out.println(zae.getName());
zae = zin.getNextZipEntry();
System.out.println(zae.getName());
zae = zin.getNextZipEntry();
System.out.println(zae.getName());
} finally {
if (zin != null) {
zin.close();
}
}