2023年12月7日发(作者:)

vcf通讯录格式解析

版权声明:本文为博主原创文章,未经博主允许不得转载。 /caizhigui/article/details/54666781

工具类

package ;

import rayOutputStream;

import ortedEncodingException;

import ist;

import ;

/**

* @author

chendd

* @date

2017/1/20

* @description

:解析电话薄

VCF

文件

ENCODING=QUOTED-PRINTABLE

编码代码如下

*/

public class CallingCardUtil {

static public class ContactVcf {

String eName;

String cName;

List phoneNumbers;

List notes;

public ContactVcf() {

eName = "";

cName ="";

phoneNumbers = new ArrayList();

notes =new ArrayList();

}

}

/*

* 解码

*/

public static String qpDecoding(String str)

{

if (str == null)

{

return "";

}

try

{

str = eAll("=n", "");

byte[] bytes = es("US-ASCII");

for (int i = 0; i < ; i++)

{

byte b = bytes[i];

if (b != 95)

{

bytes[i] = b;

}

else

{

bytes[i] = 32;

}

}

if (bytes == null)

{

return "";

}

ByteArrayOutputStream buffer = new ByteArrayOutputStream();

for (int i = 0; i < ; i++) for (int i = 0; i < ; i++) { int b = bytes[i]; if (b == '=') { try { int u = ((char) bytes[++i], 16); int l = ((char) bytes[++i], 16); if (u == -1 || l == -1) { continue; } ((char) ((u << 4) + l)); } catch (ArrayIndexOutOfBoundsException e) { tackTrace(); } } else { (b); } } return new String(Array(), "UTF-8"); } catch (Exception e) { tackTrace(); return ""; } }/* * 编码 */ public static String qpEncodeing(String str) { char[] encode = Array(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < ; i++) { if ((encode[i] >= '!') && (encode[i] <= '~') && (encode[i] != '=') && (encode[i] != 'n')) { (encode[i]); } else if (encode[i] == '=') { ("=3D"); } else if (encode[i] == 'n') { ("n"); } else { StringBuffer sbother = new StringBuffer(); (encode[i]); String ss = ng(); byte[] buf = null; try { buf = es("utf-8"); }

}

catch (UnsupportedEncodingException e)

{

tackTrace();

}

if ( == 3)

{

for (int j = 0; j < 3; j++)

{

String s16 = f(tring(buf[j]));

// 抽取中文字符16进制字节的后两位,也就是=E8等号后面的两位,

// 三个代表一个中文字符

char c16_6;

char c16_7;

if ((6) >= 97 && (6) <= 122)

{

c16_6 = (char) ((6) - 32);

}

else

{

c16_6 = (6);

}

if ((7) >= 97 && (7) <= 122)

{

c16_7 = (char) ((7) - 32);

}

else

{

c16_7 = (7);

}

("=" + c16_6 + c16_7);

}

}

}

}

return ng();

}

static public tVcf str2ContactVcf(String vcfContent) {

String[] contents =("n");

tVcf contactVcf = new tVcf();

for (int i =0 ;i<;i++){

// n(i);

String lineContent =contents[i];

if (().equals("BEGIN:VCARD")){

continue;

}

if (().equals("END:VCARD")){

continue;

}

if (().startsWith("VERSION")){

continue;

}

analysisLineContent((),contactVcf);

// n("当前:"+());

}

// n(new Gson().toJson(contactVcf));

return contactVcf;

}

}

static private void analysisLineContent(String lineContent ,tVcf contactVcf) {

String[] lineContentSub=null;

if (().startsWith("N;")){ //英文名字

lineContentSub = ("CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:;");

if (<2){

return;

}

=ding(lineContentSub[1].split(";")[0]);

// n("英文名:"+ ding(lineContentSub[1].split(";")[0]));

}else if (().startsWith("FN;")){ //中文名字

lineContentSub = ("CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:");

if (<2){

return;

}

=ding(lineContentSub[1].split(";")[0]);

// n("中文文名:"+lineContentSub[1].split(";")[0]);

// n("中文文名:"+ ding(lineContentSub[1].split(";")[0]) );

}else if (().startsWith("TEL;")){ //电话号码

lineContentSub = ("CELL:");

if (<2){

return;

}

(lineContentSub[1]);

}else if (().startsWith("NOTE;")){ //备注

lineContentSub = ("ENCODING=QUOTED-PRINTABLE:");

if (<2){

return;

}

String note =ding( lineContentSub[1].trim());

(note);

}

}

}

测试类package ;import ;import ;import Util;/** * Created by czg on 2016/10/10. */public class CallingCardUtilTest { @Test public void str2ContactVcfTest(){ StringBuilder stringBuilder = new StringBuilder(); String vcfContent = new String(tesFromStream(getClass().getResourceAsStream("/"))); tVcf contactVcf=2ContactVcf(vcfContent); n(new Gson().toJson(contactVcf)); } @Test public void qpDecodingTest() { String s =ding("=E4=BA=B2=E4=BA=BA=E8=94=A1=E5=B0=91=E4=BF=8A"); n(s); s =ding("=E4=BA=B2=E4=BA=BA=E8=94=A1=E5=B0=91=E4=BF=8A"); n(s); s =ding("=E8=B4=A2=E5=8A=A1=E7=A7=91=31=30=E6=A5=BC"); n(s); }}