[C#,Java,PHP] - IMAP文件夹名称编码和解码方法
2012-03-01 09:57
363 查看
[C#] 来源:http://www.oschina.net/code/snippet_110991_2237
[Java] 来源:/article/4157267.html
[PHP] 来源:从上面C#的代码翻译过来
// 编码 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] 来源:/article/4157267.html
// 编码 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; }
相关文章推荐
- Atitit. 二进制数据ascii表示法,与base64编码解码api 设计标准化总结java php c#.net
- Atitit. 二进制数据ascii表示法,与base64编码解码api 设计标准化总结java php c#.net
- c#中自定义Base16编码解码的方法示例
- Atitit.提升软件Web应用程序 app性能的方法原理 h5 js java c# php python android .net
- java.net.URLEncoder和java.net.URLDecoder的使用和js 中编码(encode)和解码(decode)方法
- C#和Js 编码和解码方法
- C#中Base64之编码,解码方法
- C#中Base64之编码,解码方法
- java、php、C#、asp实现短信群发功能的方法
- summernote图片上传功能保存到服务器指定文件夹+php代码+java方法
- C#解码base64编码二进制数据的方法
- 解析关于java,php以及html的所有文件编码与乱码的处理方法汇总
- IMAP文件夹编码解码
- C#中Base64之编码,解码方法
- C#中Base64之编码,解码方法
- Java编程---4、用递归的方法查询出某一文件夹下所有文件夹和文件的名称
- Java获取文件夹下所有文件名称的方法示例
- JS的encodeURI编码后,PHP解码方法
- 解析关于java,php以及html的所有文件编码与乱码的处理方法汇总
- C#中Base64之编码,解码方法