1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
using System;
using System.Net.Sockets;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
using System.Text;
namespace WindowsApplication1
{
/// <summary>
/// Summary description for JointPiece.
/// </summary>
public class JoinPiece
{
// VARIABLES
ArrayList brutPieceJoin;
ArrayList header;
ArrayList codedData;
ArrayList decodedData;
string filename;
string encoding;
string localDirectory;
// CONSTRUCTEURS
public JoinPiece( )
{
this.header = new ArrayList();
this.codedData = new ArrayList();
this.decodedData = new ArrayList();
this.brutPieceJoin = new ArrayList();
}
// METHODES
public void GetData( ArrayList brutData )
{
this.brutPieceJoin = brutData;
Regex regEx = new Regex("filename=.*$");
Regex regExCoding = new Regex("^Content-Transfer-Encoding:", RegexOptions.IgnoreCase);
Regex regExFile = new Regex(@"[^?]+\?[^?]+?.*", RegexOptions.IgnoreCase);
ArrayList pointeur;
pointeur = header;
string filename;
foreach( string line in this.brutPieceJoin )
{
// Recuperation du codage du contenu
if( (pointeur == header) && (regExCoding.IsMatch(line) ) )
this.encoding = line.Substring( "Content-Transfer-Encoding:".Length ).Trim();
// Passage à la sauvegarde du contenu codé
if( line.Equals( "" ) )
{
pointeur = this.codedData;
continue;
}
if( regEx.IsMatch( line ) && (this.filename == null) )
{
filename = regEx.Match( line ).Captures[0].ToString();
filename = filename.Replace("filename=\"", "");
this.filename = filename.Replace("\"", "");
}
pointeur.Add( line );
}
}
public static string base64Decode(string data)
{
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder utf8Decode = encoder.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(data);
int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
string result = new String(decoded_char);
return result;
}
public void Export( string localDirectory )
{
byte[] bytes;
StreamWriter str;
BinaryWriter bWr;
if( codedData.Count == 0 )
return;
if( this.filename == null )
throw new Exception("Nom de fichier de la pièce jointe introuvable");
if( this.filename.ToString().Equals("") )
throw new Exception("Nom de fichier de la pièce jointe introuvable");
str = new StreamWriter( localDirectory + Path.DirectorySeparatorChar + this.filename );
if( encoding.ToLower() == "base64")
{
bWr = new BinaryWriter( str.BaseStream );
foreach( string line in codedData)
{
bytes = Convert.FromBase64String( line );
bWr.Write( bytes );
}
}
else
{
foreach( string line in codedData)
{
str.Write( line);
}
}
str.Close();
}
}
} |
Partager