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

news/2024/7/3 3:37:46

[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;
}



转载于:https://www.cnblogs.com/hcbin/archive/2012/03/01/2375067.html


http://lihuaxi.xjx100.cn/news/279665.html

相关文章

NB-Iot烟感01:烟感探测器原理和规格

一、烟感探测器分类和工作原理 感烟探测器: 俗称 烟雾探测器&#xff0c;这个产品我们到处都可以看到&#xff0c;不管是地铁站&#xff0c;还是办公楼&#xff0c;商城&#xff0c;宾馆、商店、仓库、机房、住宅等我们抬头就可以看到。也被称为感烟式火灾探测器、烟感探测器、…

那个14岁上大学、17岁读博、24岁当教授的天才神童,如今怎么样了?

陶哲轩最令人羡慕之处&#xff0c;不在于惊人的天赋和出色的成就&#xff0c;而在于坐拥这些天才和成就的同时&#xff0c;也能成长为一个享有健康生活的快乐的“普通人”。世界上最聪明的人是谁&#xff1f;一些媒体报道可能会将这个答案引向一位华裔男性——陶哲轩。据说&…

时隔15 年,Glibc引入Linux的arc4random函数

时隔 15 年&#xff0c;Glibc 终于引入了用于 Linux 的 arc4random 函数,glibc是GNU发布的libc库&#xff0c;即c运行库。glibc是linux系统中最底层的api&#xff0c;几乎其它任何运行库都会依赖于glibc。 近日的一个提交显示&#xff0c;GNU C 库 (Glibc) 终于添加了用于 Linu…

对抗图像和攻击在Keras和TensorFlow上的实现

点击上方“小白学视觉”&#xff0c;选择加"星标"或“置顶”重磅干货&#xff0c;第一时间送达本文转自&#xff1a;计算机视觉联盟AI博士笔记系列推荐周志华《机器学习》手推笔记正式开源&#xff01;可打印版本附pdf下载链接[ 摘要 ]在这篇教程中&#xff0c;你将会…

java中Volatile修饰符的含义

在java语言中&#xff1a;为了获得最佳速度&#xff0c;同意线程保存共享成员变量的私有拷贝。并且仅仅当线程进入或者离开同步代码块时才与共享成员变量的原始值进行对照。 volatilekeyword的作用就是提示vm&#xff1a;对于这个成员变量不能保存它的私有拷贝。而应直接与共享…

边界框的回归策略搞不懂?算法太多分不清?看这篇就够了

作者 | fivetrees来源 | https://zhuanlan.zhihu.com/p/76477248本文已由作者授权&#xff0c;未经允许&#xff0c;不得二次转载【导读】目标检测包括目标分类和目标定位 2 个任务&#xff0c;目标定位一般是用一个矩形的边界框来框出物体所在的位置&#xff0c;关于边界框的回…

你的Redis怎么持久化的

引言(本文改编自生活真实案例&#xff0c;如有类同&#xff0c;绝不是巧合&#xff01;)端午节&#xff0c;烟哥正在一边愉快的学习….突然&#xff0c;微信一阵抖动。原来是老刘呼唤烟哥&#xff01;善良的烟哥本以为人家是要约我出去玩&#xff01;然而&#xff0c;打开微信一…

机械自动化专业转 PLC 还是单片机,路会更好走一些?

大家好&#xff0c;我是无际。 我是电气工程专业转的单片机开发。 一、浅谈机械 说到机械自动化行业&#xff0c;我虽然没有做过&#xff0c;但是有时候做产品需要跟做机械的哥们打交道。 一回生二回熟&#xff0c;我们聊天基本也会相互调戏&#xff0c;说真羡慕你做这个啊…