private void decode_attachment() { byte[] buf=new byte[4096]; int len; int d = geti32(); switch (d) { case ASUBJECT: len = geti32(); StreamReadBytes(buf,len); byte[] _subjectBuffer=new byte[len-1]; Array.Copy(buf,_subjectBuffer,(long)len-1); strSubject=Encoding.Default.GetString(_subjectBuffer); PrintResult("Found subject: {0}", strSubject); geti16(); /* checksum */ break; case AFILENAME: len = geti32(); StreamReadBytes(buf,len); //PrintResult("File-Name: {0}\n", buf); byte[] _fileNameBuffer=new byte[len-1]; Array.Copy(buf,_fileNameBuffer,(long)len-1); string strFileName=Encoding.Default.GetString(_fileNameBuffer); //new attachment found because attachment data goes before attachment name _attachment.FileName=strFileName; _attachment.Subject=strSubject; _attachments.Add(_attachment); geti16(); /* checksum */ break; case ATTACHDATA: len = geti32(); PrintResult("ATTACH-DATA: {0} bytes\n", len); _attachment=new TNEFAttachment(); _attachment.FileContent=new byte[len]; _attachment.FileLength=len; for (int i = 0; i < len; ) { int chunk = len-i; if (chunk > buf.Length) chunk = buf.Length; StreamReadBytes(buf,chunk); Array.Copy(buf,0,_attachment.FileContent,i,chunk); i += chunk; } geti16(); /* checksum */ break; default: decode_attribute(d); break; } }
/// <summary> /// save a decoded attachment to file /// </summary> /// <param name="attachment">decoded attachment</param> /// <param name="pathToSaveTo">Where to save the attachment to</param> /// <returns>true is succeded, vice versa</returns> public static bool SaveAttachment(TNEFAttachment attachment, string pathToSaveTo) { try { string strOutFile = pathToSaveTo + attachment.FileName; if(File.Exists(strOutFile)) File.Delete(strOutFile); FileStream fsData=new FileStream(strOutFile,FileMode.CreateNew,FileAccess.Write); fsData.Write(attachment.FileContent,0,(int)attachment.FileLength); fsData.Close(); return true; } catch(Exception e) { Utility.LogError("SaveAttachment():"+e.Message); return false; } }