vcf通讯录格式解析

工具类


package hd.com.xposeddemo.utils;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:chendd
 * @date:2017/1/20
 * @description:解析电话薄 VCF文件ENCODING=QUOTED-PRINTABLE编码代码如下
 */
public class CallingCardUtil {
    static public class ContactVcf {
        String eName;
        String cName;
        List<String > phoneNumbers;
        List<String > notes;

        public ContactVcf() {
            eName = "";
            cName ="";
            phoneNumbers = new ArrayList<String>();
            notes =new ArrayList<String>();
        }
    }

    /*
     * 解码
     */
    public static  String qpDecoding(String str)
    {
        if (str == null)
        {
            return "";
        }
        try
        {
            str = str.replaceAll("=\n", "");
            byte[] bytes = str.getBytes("US-ASCII");
            for (int i = 0; i < bytes.length; 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 < bytes.length; i++)
            {
                int b = bytes[i];
                if (b == '=')
                {
                    try
                    {
                        int u = Character.digit((char) bytes[++i], 16);
                        int l = Character.digit((char) bytes[++i], 16);
                        if (u == -1 || l == -1)
                        {
                            continue;
                        }
                        buffer.write((char) ((u << 4) + l));
                    }
                    catch (ArrayIndexOutOfBoundsException e)
                    {
                        e.printStackTrace();
                    }
                }
                else
                {
                    buffer.write(b);
                }
            }
            return new String(buffer.toByteArray(), "UTF-8");
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return "";
        }
    }
/*
     * 编码
     */

    public static String qpEncodeing(String str)
    {
        char[] encode = str.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < encode.length; i++)
        {
            if ((encode[i] >= '!') && (encode[i] <= '~') && (encode[i] != '=')
                    && (encode[i] != '\n'))
            {
                sb.append(encode[i]);
            }
            else if (encode[i] == '=')
            {
                sb.append("=3D");
            }
            else if (encode[i] == '\n')
            {
                sb.append("\n");
            }
            else
            {
                StringBuffer sbother = new StringBuffer();
                sbother.append(encode[i]);
                String ss = sbother.toString();
                byte[] buf = null;
                try
                {
                    buf = ss.getBytes("utf-8");
                }
                catch (UnsupportedEncodingException e)
                {
                    e.printStackTrace();
                }
                if (buf.length == 3)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        String s16 = String.valueOf(Integer.toHexString(buf[j]));
                        // 抽取中文字符16进制字节的后两位,也就是=E8等号后面的两位,
                        // 三个代表一个中文字符
                        char c16_6;
                        char c16_7;
                        if (s16.charAt(6) >= 97 && s16.charAt(6) <= 122)
                        {
                            c16_6 = (char) (s16.charAt(6) - 32);
                        }
                        else
                        {
                            c16_6 = s16.charAt(6);
                        }
                        if (s16.charAt(7) >= 97 && s16.charAt(7) <= 122)
                        {
                            c16_7 = (char) (s16.charAt(7) - 32);
                        }
                        else
                        {
                            c16_7 = s16.charAt(7);
                        }
                        sb.append("=" + c16_6 + c16_7);
                    }
                }
            }
        }
        return sb.toString();
    }



    static public CallingCardUtil.ContactVcf str2ContactVcf(String vcfContent) {
        String[] contents =vcfContent.split("\n");
        CallingCardUtil.ContactVcf contactVcf = new CallingCardUtil.ContactVcf();
        for (int i =0 ;i<contents.length;i++){
//            System.out.println(i);

            String lineContent =contents[i];

            if (lineContent.trim().equals("BEGIN:VCARD")){
                continue;
            }
            if (lineContent.trim().equals("END:VCARD")){
                continue;
            }
            if (lineContent.trim().startsWith("VERSION")){
                continue;
            }
            analysisLineContent(lineContent.trim(),contactVcf);
//            System.out.println("当前:"+lineContent.trim());


        }
//        System.out.println(new Gson().toJson(contactVcf));
        return contactVcf;

    }

    static private   void analysisLineContent(String lineContent ,CallingCardUtil.ContactVcf contactVcf) {
        String[] lineContentSub=null;
        if (lineContent.trim().startsWith("N;")){  //英文名字
            lineContentSub = lineContent.split("CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:;");
            if (lineContentSub.length<2){
                return;
            }
            contactVcf.eName =CallingCardUtil.qpDecoding(lineContentSub[1].split(";")[0]);
//            System.out.println("英文名:"+  CallingCardUtil.qpDecoding(lineContentSub[1].split(";")[0]));

        }else if (lineContent.trim().startsWith("FN;")){  //中文名字
            lineContentSub = lineContent.split("CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:");
            if (lineContentSub.length<2){
                return;
            }
            contactVcf.cName =CallingCardUtil.qpDecoding(lineContentSub[1].split(";")[0]);
//            System.out.println("中文文名:"+lineContentSub[1].split(";")[0]);
//            System.out.println("中文文名:"+ CallingCardUtil.qpDecoding(lineContentSub[1].split(";")[0]) );
        }else if (lineContent.trim().startsWith("TEL;")){  //电话号码
            lineContentSub = lineContent.split("CELL:");
            if (lineContentSub.length<2){
                return;
            }
            contactVcf.phoneNumbers.add(lineContentSub[1]);
        }else if (lineContent.trim().startsWith("NOTE;")){  //备注
            lineContentSub = lineContent.split("ENCODING=QUOTED-PRINTABLE:");
            if (lineContentSub.length<2){
                return;
            }
            String note =CallingCardUtil.qpDecoding( lineContentSub[1].trim());
            contactVcf.notes.add(note);
        }

    }

}


测试类


package hd.com.xposeddemo.utils;

import com.google.gson.Gson;

import org.junit.Test;

import hd.com.xposeddemo.StreamUtil;


/**
 * Created by czg on 2016/10/10.
 */
public class CallingCardUtilTest {

    @Test
    public void str2ContactVcfTest(){
        StringBuilder stringBuilder = new StringBuilder();



        String  vcfContent = new String(StreamUtil.readBytesFromStream(getClass().getResourceAsStream("/contactCallingCard.vcf")));
        CallingCardUtil.ContactVcf contactVcf=CallingCardUtil.str2ContactVcf(vcfContent);
        System.out.println(new Gson().toJson(contactVcf));




    }

    @Test
    public void qpDecodingTest() {
        String s =CallingCardUtil.qpDecoding("=E4=BA=B2=E4=BA=BA=E8=94=A1=E5=B0=91=E4=BF=8A");
        System.out.println(s);

        s =CallingCardUtil.qpDecoding("=E4=BA=B2=E4=BA=BA=E8=94=A1=E5=B0=91=E4=BF=8A");
        System.out.println(s);
        s =CallingCardUtil.qpDecoding("=E8=B4=A2=E5=8A=A1=E7=A7=91=31=30=E6=A5=BC");
        System.out.println(s);




    }



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牵手生活

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值