[英]Decode koi8-r string and quoted-printable in java
我有一个.eml 文件,里面有一些附件
附件之一 - is.rar 文件
我用tika解压这个rar,但是有时候tika不能正确转换一些文件名,比如——这样的名字
=?koi8-r?Q?6=5F=F4=ED=5F15=2E05=2Erar?=
所以我一直在寻找答案,如何将这样的字符串转换为正确读取的值
java 中是否有任何库可以执行此操作?
我猜它会导致字符串得到=?koi8-r?Q?
一开始,所以也许,如果我将字符串转换成这样的东西,我会得到移动可转换值,就像这样6=5F=F4=ED=5F15=2E05=2E
,但如果我这样做,我最终找不到转换的解决方案
有人知道如何正确转换这样的字符串吗?
我花了很多时间来完成它,但仍然 - 没有结果......
这是一个代码
public class EncodingUtils {
private EncodingUtils() {
}
public static String decodeKoi8r(String text) {
String decode;
try {
decode = MimeUtility.decodeText(text);
} catch (UnsupportedEncodingException e) {
decode = text;
}
if (isQuotedKoi8r(decode)) {
decode = decode(text, "KOI8-R", "quoted-printable", "KOI8-R");
}
return decode;
}
public static boolean isQuotedKoi8r(String text) {
return text.contains("=") || text.toLowerCase().contains("koi8-r");
}
public static String decode(String text, String textEncoding, String encoding, String resultCharset) {
if (text.length() == 0) {
return text;
}
try {
byte[] bytes = text.getBytes(textEncoding);
InputStream decodedStream = MimeUtility.decode(new ByteArrayInputStream(bytes), encoding);
byte[] tmp = new byte[bytes.length];
int n = decodedStream.read(tmp);
byte[] res = new byte[n];
System.arraycopy(tmp, 0, res, 0, n);
return new String(res, resultCharset);
} catch (IOException | MessagingException e) {
return text;
}
}
}
并测试:
public class EncodingUtilsTest {
@Test
public void koi8r() {
String input = "=?koi8-r?Q?11=5F=F4=ED=5F21=2E05=2Erar?=";
String decode = EncodingUtils.decodeKoi8r(input);
Assertions.assertEquals("11_ТМ_21.05.rar", decode);
}
@Test
public void koi8rWithoutStartTag() {
String input = "=CF=D4=C4=C5=CC=D8=CE=D9=CD =D4=D2=C1=CE=DB=C5=CD =D2=C5=DA=C0=CD=.eml";
String decode = EncodingUtils.decodeKoi8r(input);
Assertions.assertEquals("отдельным траншем резюм=.eml", decode);
}
}
再会!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.