Using Node.js to convert file encoding formats

php中世界最好的语言
Release: 2018-06-04 15:05:41
Original
2637 people have browsed it

这次给大家带来利用Node.js进行文件编码格式转换,利用Node.js进行文件编码格式转换的注意事项有哪些,下面就是实战案例,一起来看一下。

ASCII编码就比较蛋疼,通过搜索网上资源,反复测试对比,最终形成下面比较靠谱的方法(有一些 EditPlus显示编码为utf-8但node.js库返回的却是其它编码>_<)

判断修改是否无误,只需要在修改完之后,通过SVN提交,浏览提交列表,双击任意一项待提交文件,如果显示下图所示的对话框,则说明修改成功,其它都会看到中文反而变成乱码了

var fs = require('fs');
var chardet = require('chardet');
var jschardet = require("jschardet");
var encoding = require("encoding");
var path = "lua目录";
function readDirectory(dirPath) {
  if (fs.existsSync(dirPath)) {
    var files = fs.readdirSync(dirPath);
    files.forEach(function (file) {
      var filePath = dirPath + "/" + file;
      var stats = fs.statSync(filePath);
      if (stats.isDirectory()) {
        // console.log('/n读取目录:\n', filePath, "\n");
        readDirectory(filePath);
      } else if (stats.isFile() && /\.lua$/.test(filePath)) {
        var buff = fs.readFileSync(filePath);
        if (buff.length && buff[0].toString(16).toLowerCase() == "ef" && buff[1].toString(16).toLowerCase() == "bb" && buff[2].toString(16).toLowerCase() == "bf") {
          //EF BB BF 239 187 191
          console.log('\n发现BOM文件:', filePath, "\n");
          buff = buff.slice(3);
          fs.writeFile(filePath, buff.toString(), "utf8");
        }
        // { encoding: 'UTF-8', confidence: 0.99 }
        // var charset = chardet.detectFileSync(filePath);
        var info = jschardet.detect(buff);
        if (info.encoding == "GB2312" || info.encoding == "ascii") {
          var resultBuffer = encoding.convert(buff, "UTF-8", info.encoding);
          fs.writeFile(filePath, resultBuffer, "utf8");
        }
        else if (info.encoding != "UTF-8" && chardet.detectFileSync(filePath) != "UTF-8")
        {
          if (buff.toString().indexOf("\r\n") > -1)
          {
            var resultBuffer = encoding.convert(buff, "UTF-8", "GBK");
            fs.writeFile(filePath, resultBuffer, "utf8");
          }
        }
      }
    });
  } else {
    console.log('Not Found Path : ', dirPath);
  }
}
readDirectory(path);

注意上面的判断,第一个明确是 GB2312或者ascii时,直接将相应的编码转为 utf-8。而如果返回是格式,先判断是否有PC下的换行符,如果有则全部将它视为GBK进行处理。

整个思路其实是比较简单,难点在于如果判断文件编码格式。这个真的很难>_<,获取原编码格式后,调用 encoding.convert(buff, 目标编码格式 , 原始编码格式 ); 便可得到所需要的编码。如果有空而且有兴趣,可以下载Notepad++的源码,看它是如何判断文件的编码格式

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

js基础提升学习之三种内置对象

js基础提升学习之基本数据类型

The above is the detailed content of Using Node.js to convert file encoding formats. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template