I've created a small C# application which extracts attachments from outlook emails and saves them to a network location. I can open all of the saved files except for compressed files with "zip" extension.
Files with the "zip" extension can only be opened/extracted via Windows Explorer. If I try to use WinRAR, I get the following error: "The archive is either in unknown format or damaged". WinZip does not work either. If however, I open the email within outlook manually and copy the zip file to my PC, I am able to open the zipped file using WinRAR, WinZip as well as windows explorer.
Part of the code used to save the attachment is shown below.
Microsoft.Office.Interop.Outlook.MailItem _mailItem;
...
...
_AttachFileName = _mailItem.Attachments[j].FileName;
_TempAttachmentFile = Path.Combine(_tempFolder.Path, _AttachFileName);
_mailItem.Attachments[j].SaveAsFile(_TempAttachmentFile);
...
...
...
...
_AttachFileName = _mailItem.Attachments[j].FileName;
_TempAttachmentFile = Path.Combine(_tempFolder.Path, _AttachFileName);
_mailItem.Attachments[j].SaveAsFile(_TempAttachmentFile);
...
...
Has anyone faced a similar issue with compressed zip files? If so were you able to resolve the issue?
Any ideas or help on this will be appreciated.
Thanks
Navnit
navnit mudaliarPosted May 11, 2011, 11:47 PM
I managed to get to the bottom of the issue. The problem I was having wasn't caused by the code I posted earlier.
After posting my issue on the forum, I had a closer look at what I was doing within my code and found that at a later stage of my application code, I was converting the attachment into base64 string for some internal processing. This was eventually getting saved back as a file to the final destination folder. It was during this final saving process I accidently reduced the data by a byte.
Code causing the issue:
FileStream outFile;
..
outFile.Write(binaryData, 0, binaryData.Length-1);
..
Fixed code:
FileStream outFile;
..
outFile.Write(binaryData, 0, binaryData.Length);
..
Apologies for the confusion.