[C #] Source: http://www.oschina.net/code/snippet_110991_2237
// Encoding
Private String Imapencode ( String Folder)
{
String RTN = "" , Base64;
Int Index = 0 ;
RegEx regasis = New RegEx (@" \ G (? : [\ X20-\ x25 \ x27-\ x7e]) + " );
RegEx reg26 = New RegEx ( @" \ G & " );
RegEx regencode = New RegEx ( @" \ G (? : [^ \ X20-\ x7e]) + " );
RegEx regeq = New RegEx (@" = + $ " );
RegEx regslash = New RegEx ( @" \/ " );
While (Index <folder. Length ){
Match m;
M = regasis. Match (folder, index );
If (M. Success)
{
Index = index + M. length;
RTN = RTN + M. value;
Continue ;
}
M = reg26.match (folder, index );
If (M. Success)
{
Index = index + M. length;
RTN = RTN + " &- " ;
Continue ;
}
M = regencode. Match (folder, index );
If (M. Success)
{
Index = index + M. length;
Base64 = convert. tobase64string (encoding. getencoding ( " UTF-16BE " ). Getbytes (M. Value ));
Base64 = regeq. Replace (base64, "" );
Base64 = regslash. Replace (base64, " , " );
RTN = RTN + " & " + Base64 +" - " ;
Continue ;
}
}
Return RTN;
}
// Decoding
Private String Imapdeconde ( String Folder)
{
String RTN = "" , Base64;
Int Index = 0 ;
RegEx regasis = New RegEx ( @" \ G ([^ &] +) " );
RegEx reg26 = New RegEx ( @" \ G \&- " );
RegEx regdecode =New RegEx ( @" \ G \ & ([A-Za-z0-9 +,] + )-? " );
RegEx regcomma = New RegEx ( @" , " );
While (Index <folder. Length ){
Match m;
M = regasis. Match (folder, index );
If (M. Success)
{
Index = index + M. length;
RTN = RTN + M. value;
Continue ;
}
M = reg26.match (folder, index );
If (M. Success)
{
Index = index + M. length;
RTN = RTN + " & " ;
Continue ;
}
M = regdecode. Match (folder, index );
If (M. Success)
{
Index = index + M. length;
Base64 = M. value. substring ( 1 , M. value. Length- 2 );
Base64 = regcomma. Replace (base64, " / " );
Int MoD = base64.length % 4 ;
If (Mod> 0 ) Base64 = base64.padright (base64.length + ( 4 -Mod ), ' = ' );
Base64 = encoding. getencoding ( " UTF-16BE " ). Getstring (convert. frombase64string (base64 ));
RTN = RTN + base64;
Continue ;
}
}
Return RTN;
}
[Java] Source: http://grandboy.iteye.com/blog/908887
// Encoding
Public Class Imapfolderencoder {
Public Static String encode (string folder ){
String RTN = "", base64;
Int Index = 0;
Pattern regasis = pattern. Compile ("\ G (? : [\ X20-\ x25 \ x27-\ x7e]) + ");
Pattern reg26 = pattern. Compile ("\ G &");
Pattern regencode = pattern. Compile ("\ G (? : [^ \ X20-\ x7e]) + ");
Pattern regeq = pattern. Compile ("= + $ ");
Pattern regslash = pattern. Compile ("\\/");
While (Index <folder. Length ()){
Matcher m;
M = regasis. matcher (folder );
If (M. Find (INDEX )){
Index = index + (M. End ()-M. Start ());
RTN = RTN + M. Group ();
Continue ;
}
M = reg26.matcher (folder );
If (M. Find (INDEX )){
Index = index + (M. End ()-M. Start ());
RTN = RTN + "&-";
Continue ;
}
M = regencode. matcher (folder );
If (M. Find (INDEX )){
Index = index + (M. End ()-M. Start ());
Base64 = simpleutil. encodebase64content (M. Group (), "UTF-16BE ");
Base64 = base64.replaceall (regeq. pattern (),"");
Base64 = base64.replaceall (regslash. pattern (),",");
RTN = RTN + "&" + base64 + "-";
Continue ;
}
}
Return RTN;
}
}
// Decoding
Public Class Imapfolderdecoder {
Public Static String decode (string folder ){
String RTN = "", base64;
Int Index = 0;
Pattern regasis = pattern. Compile ("\ G ([^ &] + )");
Pattern reg26 = pattern. Compile ("\ G \\&-");
Pattern regdecode = pattern. Compile ("\ G \ & ([A-Za-z0-9 +,] + )-? ");
Pattern regcomma = pattern. Compile (",");
While (Index <folder. Length ()){
Matcher m;
M = regasis. matcher (folder );
If (M. Find (INDEX )){
Index = index + (M. End ()-M. Start ());
RTN = RTN + M. Group ();
Continue ;
}
M = reg26.matcher (folder );
If (M. Find (INDEX )){
Index = index + (M. End ()-M. Start ());
RTN = RTN + "&";
Continue ;
}
M = regdecode. matcher (folder );
If (M. Find (INDEX )){
Index = index + (M. End ()-M. Start ());
Base64 = M. Group (). substring (1, M. Group (). Length ()-1 );
Base64 = base64.replaceall (regcomma. pattern (),"/");
Int MoD = base64.length () % 4;
Int Count = 4-MOD;
While (Count> 0 ){
Base64 + = "= ";
Count --;
}
Base64 = simpleutil. base64decode (base64, "UTF-16BE ");
RTN = RTN + base64;
Continue ;
}
}
Return RTN;
}
}
[PHP] Source: From the above C #CodeTranslated
// Encoding
Function Imapencode ( $ SSTR )
{
$ Sout = '';
$ Sbase64 = '';
$ Nindex = 0;
$ Sregasis = '/^ (? : [\ X20-\ x25 \ x27-\ x7e]) + /';
$ Sreg26 = '/^ &/';
$ Sregencode = '/^ (? : [^ \ X20-\ x7e]) + /';
$ Sregeq = '/= + $ /';
$ Sregslash = '/\//';
While ( $ Nindex < Strlen ( $ SSTR ))
{
$ Atmp = Array ();
$ Nresult =Preg_match ( $ Sregasis , $ SSTR ,& $ Atmp , Null , $ Nindex );
If ( $ Nresult )
{
$ Nindex + = Strlen ( $ Atmp [0]);
$ Sout . =$ Atmp [0];
Continue ;
}
$ Nresult = Preg_match ( $ Sreg26 , $ SSTR ,& $ Atmp , Null , $ Nindex );
If ( $ Nresult )
{
$ Nindex + = Strlen ( $ Atmp [0]);
$ Sout . = '&-';
Continue ;
}
$ Nresult = Preg_match ( $ Sregencode , $ SSTR ,& $ Atmp , Null , $ Nindex );
If ( $ Nresult )
{
$ Nindex + = Strlen ( $ Atmp [0]);
$ Sbase64 = Base64_encode ( Iconv ('Utf-8', 'utf-16be ', $ Atmp [0]);
$ Sbase64 = Preg_replace ($ Sregeq ,'', $ Sbase64 );
$ Sbase64 = Preg_replace ( $ Sregslash ,',', $ Sbase64 );
$ Sout . = '&'. $ Sbase64 .'-';
Continue ;
}
}
Return $ Sout ;
}
// Decoding
Function Imapdeconde ( $ SSTR )
{
$ Sout = '';
$ Sbase64 = '';
$ Nindex = 0;
$ Sregasis = '/^ ([^ &] + )/';
$ Sreg26 = '/^ \&-/';
$ Sregdecode = '/^ \ & ([A-Za-z0-9 +,] + )-? /';
$ Sregcomma = '/,/';
While ( $ Nindex < Strlen ( $ SSTR ))
{
$ Atmp = Array ();
$ Nresult = Preg_match ( $ Sregasis , $ SSTR ,& $ Atmp , Null , $ Nindex );
If ( $ Nresult )
{
$ Nindex + = Strlen ( $ Atmp [0]);
$ Sout . = $ Atmp [0];
Continue ;
}
$ Nresult = Preg_match ( $ Sreg26 , $ SSTR ,& $ Atmp , Null , $ Nindex );
If ( $ Nresult )
{
$ Nindex + =Strlen ( $ Atmp [0]);
$ Sout . = '&';
Continue ;
}
$ Nresult = Preg_match ( $ Sregdecode , $ SSTR ,& $ Atmp , Null , $ Nindex );
If ( $ Nresult )
{
$ Nindex + = Strlen ( $ Atmp [0]);
$ Sbase64 = Substr ( $ Atmp [0], 1, Strlen ( $ Atmp [0])-2 );
$ Sbase64 = Preg_replace ( $ Sregcomma ,'/', $ Sbase64 );
$ Nmod = Strlen ( $ Sbase64 ) % 4;
If ( $ Nmod > 0) $ Sbase64 . = Str_repeat ('=', 4- $ Nmod );
$ Sbase64 = Iconv ('Utf-16be', 'utf-8 ', Base64_decode ( $ Sbase64 ));
$ Sout . = $ Sbase64 ;
Continue ;
}
}
Return $ Sout ;
}