[C#,Java,PHP] - IMAP文件夹名称编码和解码方法

[C#] 来源:http://www.oschina.net/code/snippet_110991_2237

// 编码
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;
}

// 解码
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] 来源:http://grandboy.iteye.com/blog/908887

// 编码
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;
}

}

// 解码
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] 来源:从上面C#的代码翻译过来

// 编码
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;
}

// 解码
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;
}



posted @ 2012-03-01 09:57  炎峰森林影  阅读(999)  评论(0编辑  收藏  举报