zoukankan      html  css  js  c++  java
  • Chrome书签导入IE或Maxthon的程序(三)

    上一篇介绍了Chrome书签和IE收藏夹文件的格式对应,需要先将Chrome书签文件格式转换为XML方便操作。本篇就将如何进行这种转换。

    考虑到文件转换效率,一行行读取后判断在转换的方式固然是最简单思路最清晰地,但是效率恐怕是最低下的。如果遇到收藏加层次很多文件很多的情况就很慢,因此我们选择用正则表达式替换方式对全文依次按照指定模式替换相应内容,最后生成需要的XML文件。

    这里列出我在程序中使用的正则表达式:

    "\\s*\"other\".*\\n.*\\n.*\\ {"

    ".*\\n\\s*\"date_added\".*\\n.*\\n.*\\n.*\\n.*\\n.*\\n.*\\n.*\\\\u5176\\\\u4ED6\\\\u4E66\\\\u7B7E.*\\n.*"

    \\s*\"roots\":\\s\\ {"

    "\\s*\"bookmark_bar\":\\s\\ {"

    "\\s*\"children\":\\s\\[\\s\\ {"

    "\\s*\"date_added\":\\s\"(.*)\ ","

    "\\s*\"name\":\\s\"(.*)\ ","

    "\\s*\"type\":\\s\"(.*)\ ""

    "\\s*\"url\":\\s\"(.*)\ ""

    "\\s*\"date_modified\":\\s\"(.*)\ ","

    "\\s*\"other\":\\s\\ {"

    "\\],"

    "\\{|\\ }|,"

    "\\s*\"version\":\\s1 "

    "(\\\\u ....)"

    对照源程序可以看得懂是用来替换什么部分的。值得一提的是在C#的正则表达式替换部分增加一个处理事件,可以同事将Unicode编码的名字转换为中文。

    1. private   string Unicode2GB(Match regmatch)
    2.     {
    3.       string dst = "" ;
    4.       string src = regmatch.Value;
    5.       string str = "" ;
    6.       str = src.Substring(2);
    7.       byte [] bytes = new   byte [2];
    8.       bytes[1] = byte .Parse( int .Parse(str.Substring(0, 2), NumberStyles.HexNumber).ToString());
    9.       bytes[0] = byte .Parse( int .Parse(str.Substring(2, 2), NumberStyles.HexNumber).ToString());
    10.       dst = Encoding.Unicode.GetString(bytes);
    11.       return dst;
    12.     }

    最后就是导出XML格式为IE可用的收藏夹了,这部分简单,一个递归函数分析XML中的标记,然后在给定的路径创建文件夹或.url文件即可。

     

    最后贴出所有的源码,大家可以参考改进,祝大家顺利!

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Text;
    7. using System.Windows.Forms;
    8. using System.IO;
    9. using System.Text.RegularExpressions;
    10. using System.Globalization;
    11. using System.Xml;
    12. namespace BookmarksTrans
    13. {
    14.   public partial class Form1 : Form
    15.   {
    16.     public Form1()
    17.     {
    18.       InitializeComponent();
    19.     }
    20.     private   void button1_Click( object sender, EventArgs e)
    21.     {
    22.       openFileDialog1.ShowDialog();
    23.       if (openFileDialog1.FileName!= "" )
    24.       {
    25.         txt_Chrome.Text = openFileDialog1.FileName;
    26.       }
    27.     }
    28.     private   void DealBookMark( string bookmarkFile)
    29.     {
    30.       XmlDocument xmlDoc = new XmlDocument();
    31.       XmlNode xmlBase;
    32.       XmlNodeList xmlURLlist;
    33.       StreamReader sdr = new StreamReader(bookmarkFile);
    34.       TreeNode tempNode = new TreeNode();
    35.       string readData = "" ;
    36.       try
    37.       {
    38.         // 处理Chrome收藏夹格式为xml
    39.         readData = sdr.ReadToEnd();
    40.         readData = Regex.Replace(readData, "\\s*\"other\".*\\n.*\\n.*\\{" , "" );
    41.         readData = Regex.Replace(readData,
    42.                                  ".*\\n\\s*\"date_added\".*\\n.*\\n.*\\n.*\\n.*\\n.*\\n.*\\n.*\\\\u5176\\\\u4ED6\\\\u4E66\\\\u7B7E.*\\n.*" ,
    43.                                  "" );
    44.         readData = Regex.Replace(readData, "&" , "@" );
    45.         readData = Regex.Replace(readData, "\\s*\"roots\":\\s\\{" ,
    46.                                  "<?xml version=\"1.0\" encoding=\"GBK\" ?>" );
    47.         readData = Regex.Replace(readData, "\\s*\"bookmark_bar\":\\s\\{" , "<bookmark>" );
    48.         readData = Regex.Replace(readData, "\\s*\"children\":\\s\\[\\s\\{" , "<bookmarkchild>" );
    49.         readData = Regex.Replace(readData, "\\s*\"date_added\":\\s\"(.*)\"," ,
    50.                                  "<date_added>$1</date_added>" );
    51.         readData = Regex.Replace(readData, "\\s*\"name\":\\s\"(.*)\"," , "<name>$1</name>" );
    52.         readData = Regex.Replace(readData, "\\s*\"type\":\\s\"(.*)\"" , "<type>$1</type>" );
    53.         readData = Regex.Replace(readData, "\\s*\"url\":\\s\"(.*)\"" , "<url>$1</url>" );
    54.         readData = Regex.Replace(readData, "\\s*\"date_modified\":\\s\"(.*)\"," ,
    55.                                  "<date_modified>$1</date_modified>" );
    56.         readData = Regex.Replace(readData, "\\s*\"other\":\\s\\{" , "<bookmarkchild>" );
    57.         readData = Regex.Replace(readData, "\\]," , "</bookmarkchild>" );
    58.         readData = Regex.Replace(readData, "\\{|\\}|," , "" );
    59.         readData = Regex.Replace(readData, "\\s*\"version\":\\s1" , "</bookmark>" );
    60.         readData = Regex.Replace(readData, "(\\\\u....)" , new MatchEvaluator(Unicode2GB));
    61.         // 导出收藏夹
    62.         xmlDoc.LoadXml(readData);
    63.         xmlBase = xmlDoc.DocumentElement;
    64.         xmlURLlist = xmlBase.ChildNodes;
    65.         tempNode.Text = "收藏夹" ;
    66.         trv_MarkbookList.Nodes.Add(tempNode);
    67.         AddBookMark(xmlURLlist,tempNode);
    68.       }
    69.       catch (Exception ex)
    70.       {
    71.         MessageBox.Show( "书签分析出错:" +ex.Message, "错误" , MessageBoxButtons.OK, MessageBoxIcon.Error);
    72.       }
    73.       finally
    74.       {
    75.         sdr.Close();
    76.         sdr.Dispose();
    77.       }
    78.     }
    79.     /// <summary>
    80.     /// Unicode编码转换为汉字
    81.     /// </summary>
    82.     /// <param name="regmatch"></param>
    83.     /// <returns></returns>
    84.     private   string Unicode2GB(Match regmatch)
    85.     {
    86.       string dst = "" ;
    87.       string src = regmatch.Value;
    88.       string str = "" ;
    89.       str = src.Substring(2);
    90.       byte [] bytes = new   byte [2];
    91.       bytes[1] = byte .Parse( int .Parse(str.Substring(0, 2), NumberStyles.HexNumber).ToString());
    92.       bytes[0] = byte .Parse( int .Parse(str.Substring(2, 2), NumberStyles.HexNumber).ToString());
    93.       dst = Encoding.Unicode.GetString(bytes);
    94.       return dst;
    95.     }
    96.     /// <summary>
    97.     /// 将具有子栏目的书签添加到TreeView中
    98.     /// </summary>
    99.     /// <param name="xmlList">子栏目</param>
    100.     private   void AddBookMark(XmlNodeList xmlList,TreeNode parentNode)
    101.     {
    102.       TreeNode PNode = parentNode;
    103.       for ( int i = 0; i < xmlList.Count - 1; i++)
    104.       {
    105.         if (xmlList[i].Name == "bookmarkchild" )
    106.         {
    107.           TreeNode sonNode = new TreeNode();
    108.           if (xmlList[i+3].InnerText != "书签栏" )
    109.           {
    110.             sonNode.Text = xmlList[i + 3].InnerText;
    111.             parentNode.Nodes.Add(sonNode);
    112.             PNode = sonNode;
    113.           }
    114.           AddBookMark(xmlList[i].ChildNodes, PNode);
    115.         }
    116.         else   if (xmlList[i].Name == "type" && xmlList[i].InnerText == "url" )
    117.         {
    118.           // 文件名非法字符处理
    119.           string urlName=xmlList[i - 1].InnerText.Replace( "." , "." ).Replace( "\\"," \");
    120.           urlName=urlName.Replace( "/" , "/" ).Replace( "<" , "<" );
    121.           urlName = urlName.Replace( ">" , ">" ).Replace( "*" , "*" ).Replace( "?" , "?" );
    122.           urlName = urlName.Replace( ":" , ":" ).Replace( "\"" , """ ).Replace( "|" , "|" );
    123.           TreeNode urlNode= new TreeNode(urlName);
    124.           urlNode.Tag = xmlList[i + 1].InnerText;
    125.           parentNode.Nodes.Add(urlNode);
    126.         }
    127.       }
    128.     }
    129.     private   void btn_DealChrome_Click( object sender, EventArgs e)
    130.     {
    131.       if (txt_Chrome.Text.Trim()== "" )
    132.       {
    133.         MessageBox.Show( "不给出Chrome书签位置,我也帮不了你!" , "不耐烦" );
    134.         return ;
    135.       }
    136.       DealBookMark(txt_Chrome.Text);
    137.     }
    138.     private   void btn_InIE_Click( object sender, EventArgs e)
    139.     {
    140.       string iebookmarkPath = "" ;
    141.       if (txt_IEbookmark.Text!= "" )
    142.       {
    143.         iebookmarkPath = txt_IEbookmark.Text;      
    144.       }
    145.       else
    146.       {
    147.         iebookmarkPath = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
    148.       }
    149.       DirectoryInfo favorite = Directory.CreateDirectory(iebookmarkPath);
    150.       ExportBookMarks(trv_MarkbookList.Nodes,favorite);
    151.       MessageBox.Show( "成功完成书签导出" );
    152.     }
    153.     /// <summary>
    154.     /// 导出书签到指定文件夹
    155.     /// </summary>
    156.     /// <param name="folderpath"></param>
    157.     private   void ExportBookMarks(TreeNodeCollection Nodes,DirectoryInfo foldInfo)
    158.     {
    159.       for ( int i = 0; i < Nodes.Count; i++)
    160.       {
    161.         if (Nodes[i].Nodes.Count>0)
    162.         {
    163.           DirectoryInfo fold =foldInfo.CreateSubdirectory(Nodes[i].Text);
    164.           ExportBookMarks(Nodes[i].Nodes,fold);
    165.         }
    166.         else
    167.         {
    168.           FileStream fileSW = File.Open(foldInfo.FullName + "\\" + Nodes[i].Text + " .url",FileMode.Create);
    169.           StreamWriter stWriter = new StreamWriter(fileSW);
    170.           try
    171.           {
    172.             stWriter.WriteLine( "[InternetShortcut]" );
    173.             stWriter.WriteLine( "URL=" + Nodes[i].Tag.ToString());
    174.             stWriter.Flush();
    175.            
    176.           }
    177.           catch (Exception ex)
    178.           {
    179.             MessageBox.Show( "导出书签出错:" + ex.Message, "错误" , MessageBoxButtons.OK, MessageBoxIcon.Error);
    180.           }
    181.           finally
    182.           {
    183.             stWriter.Close();
    184.             stWriter.Dispose();
    185.             fileSW.Close();
    186.             fileSW.Dispose();
    187.           }
    188.         }
    189.       }
    190.     }
    191.     private   void btn_IE_Click( object sender, EventArgs e)
    192.     {
    193.       folderBrowserDialog1.ShowDialog();
    194.       if (folderBrowserDialog1.SelectedPath!= "" )
    195.       {
    196.         txt_IEbookmark.Text = folderBrowserDialog1.SelectedPath;
    197.       }
    198.     }
    199.   }
    200. }

    附工程文件的下载链接:http://www.rayfile.com/files/27021328-b4b5-11dd-b869-0019d11a795f/

  • 相关阅读:
    BZOJ 1412: [ZJOI2009]狼和羊的故事
    Bzoj 2443: [Usaco2011 Open]奇数度数
    Bzoj 1101: [POI2007]Zap
    BZOJ 2186: [Sdoi2008]沙拉公主的困惑
    BZOJ 4804: 欧拉心算 欧拉函数
    Luogu P3121 [USACO15FEB]审查(黄金)Censoring (Gold)
    Luogu P3000 [USACO10DEC]牛的健美操Cow Calisthenics
    BZOJ 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛
    BZOJ 3297: [USACO2011 Open]forgot
    BZOJ 2456: mode
  • 原文地址:https://www.cnblogs.com/tristinjet/p/1598986.html
Copyright © 2011-2022 走看看