我想我找到了一个足够简单的解决方案,因为没有人回应。希望有人发现这个小脚本有助于转换 UTF7
>z='Συστήματα_Ανίχνευσης_Εισ & related security.pdf'
>encode_imap_utf7(z)
"&A6MDxQPDA8QDtwMBA7wDsQPEA7E-_&A5EDvQO5AwEDxwO9A7UDxQPDA7cDwg-_&A5UDuQPD- &- related security.pdf"
>decode_imap_utf7(encode_imap_utf7(z))
"Συστήματα_Ανίχνευσης_Εισ & related security.pdf"
>decode_imap_utf7(encode_imap_utf7(z)) == z
true
/* Se RFC 2060 - no / ~ \ in folder names */
function ureplacer(pmatch) {
var ret = ""
pmatch = pmatch.replace(/\,/g,'/')
var ix = pmatch.substr(1,pmatch.length-2)
if (ix.length % 4 != 0)
ix = ix.padEnd(ix.length+ 4 - ix.length % 4,"=")
try {
var dx = atob(ix)
for (var j = 0; j < dx.length; j = j+2) {
ret = ret + String.fromCharCode((dx.charCodeAt(j) << 8) + dx.charCodeAt(j+1))
}
} catch(err) {
console.log("Error in decoding foldername IMAP UTF7, sending empty string back")
console.log(err)
ret = ""
}
return ret
}
function breplacer(umatch) {
var bst = ""
for (var i=0; i < umatch.length; i++) {
var f = umatch.charCodeAt(i)
bst = bst + String.fromCharCode(f >> 8) + String.fromCharCode(f & 255)
}
try {
bst = '&'+btoa(bst).replace(/\//g,',').replace(/=+/,'')+'-'
}catch(err) {
console.log("Error in encoding foldername IMAP UTF7, sending empty string back")
console.log(err)
bst = ""
}
return bst
}
function decode_imap_utf7(mstring) {
var stm = new RegExp(/(\&[A-Za-z0-9\+\,]+\-)/,'g')
return mstring.replace(stm,ureplacer).replace('&-','&')
}
function encode_imap_utf7(ustring) {
ustring = ustring.replace(/\/|\~|\\/g,'')
var vgm = new RegExp(/([^\x20-\x7e]+)/,'g')
return ustring.replace('&','&-').replace(vgm,breplacer)
}