nodejs转换文件编码,使用iconv-lite插件

const fs = require("fs");
const path = require("path");
const os = require("os");
const platform = os.platform();
// npm转码插件
const iconv = require("iconv-lite");
// 注意:fs模块一般情况下默认以utf8格式输入输出

// 判断文件有无内容
function isEmpty(filepath) {
  try {
    let content = fs.readFileSync(filepath);
    return content.toString().trim().length === 0;
  } catch (error) {
    console.log(error);
    return;
  }
}

// 文本文件另存为utf8格式
function convert(filepath, filename) {
  return new Promise((resolve, reject) => {
    fs.readFile(filepath, (err, buf) => {
      if (err) {
        console.log(err.stack);
      }
      try {
        // 空文件写入文件名作为文件内容
        if (isEmpty(filepath)) {
          // windows环境需加上BOM头字符,linux不需要
          if (platform.includes("win32")) {
            fs.writeFileSync(filepath, "\ufeff" + filename);
          } else {
            fs.writeFileSync(filepath, filename);
          }
        } else {
          // 判断是否为utf8格式
          if (
            (buf[0] === 0xef && buf[1] === 0xbb) ||
            (buf[0] === 0xfe && buf[1] === 0xff) ||
            (buf[0] === 0xff && buf[1] === 0xfe)
          ) {
            console.log(`utf8文件---${filename}`);
          } else {
            // "字节(Buffer)"内容转化为gbk字符串
            const bufgbk = iconv.decode(buf, "gbk");
            // gbk字符串转化为utf8字符串
            // windows环境需加上BOM头字符,linux不需要
            let newContent = "";
            if (platform.includes("win32")) {
              newContent = "\ufeff" + bufgbk.toString("utf8");
            } else {
              newContent = bufgbk.toString("utf8");
            }
            // 原文件另存为utf8格式(覆写)
            fs.writeFileSync(filepath, newContent);
            console.log(`已转码---${filename}`);
          }
        }
      } catch (error) {
        console.log(error);
      }
      // 一定要有resolve,不然异步状态无法凝固,使用await时会失效
      resolve();
      return;
    });
  });
}

// 扫描文件夹
async function scan() {
  try {
    let fileColection = fs.readdirSync("./txt");
    const fileColectionlength = fileColection.length;
    let txtfileColection = fileColection.filter((item) =>
      path.extname(item).includes("txt")
    );
    const txtfileColectionlength = txtfileColection.length;
    // 文本文件全部转码
    for (let index = 0; index < txtfileColectionlength; index++) {
      await convert(
        path.resolve(__dirname, `./txt/${txtfileColection[index]}`),
        txtfileColection[index]
      );
    }
    // 全部文件汇总为一个大文本文件;
    for (let index = 0; index < fileColectionlength; index++) {
      let text = fs.readFileSync(
        path.resolve(__dirname, `./txt/${fileColection[index]}`)
      );
      // 文本之间追加分隔线和换行符
      const deliverStr =
        "\r\n--------------------------------------------------------------\r\n";
      let newText = Buffer.concat([text, Buffer.from(deliverStr)]);
      fs.writeFileSync("./result.txt", newText, { flag: "a+" });
    }
  } catch (error) {
    console.log(error);
  }
}

scan();

你可能感兴趣的:(nodejs,javascript,npm,前端)