Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在节点中读取使用某种ASCII扩展名(ISO-8859-1)编码的文件?_Node.js_Encoding_Fs - Fatal编程技术网

Node.js 如何在节点中读取使用某种ASCII扩展名(ISO-8859-1)编码的文件?

Node.js 如何在节点中读取使用某种ASCII扩展名(ISO-8859-1)编码的文件?,node.js,encoding,fs,Node.js,Encoding,Fs,在用fs读取文件之前,我使用chardet检测文件的编码。直到今天,我的应用程序读取的所有文件都是UTF-8或UTF-16LE。从chardet映射到节点BufferEncoding很容易。我在上面的代码图中使用了chardetToFsEncodings const chartdetToFsEncodings = new Map<string, BufferEncoding>([ ["UTF-8", "utf8"], ["UT

在用fs读取文件之前,我使用chardet检测文件的编码。直到今天,我的应用程序读取的所有文件都是
UTF-8
UTF-16LE
。从chardet映射到节点BufferEncoding很容易。我在上面的代码图中使用了
chardetToFsEncodings

const chartdetToFsEncodings = new Map<string, BufferEncoding>([
  ["UTF-8", "utf8"],
  ["UTF-16LE", "utf16le"],
]);

const plausableEncodings = analyse(buffer).map((match) => match.name);

const supportedEncoding = plausableEncodings.find((name) => chartdetToFsEncodings.get(name)) as BufferEncoding;
if (supportedEncoding) {
  resolve({
    path,
    data: buffer.toString(supportedEncoding),
  });
} else {
  reject(new Error("File encoding not recognized"));
}
const chartdetofsencodings=新地图([
[“UTF-8”、“utf8”],
[“UTF-16LE”、“utf16le”],
]);
const plausableEncodings=analysis(buffer.map)((match)=>match.name);
const supportedEncoding=plausableEncodings.find((名称)=>chartdetToFsEncodings.get(名称))作为缓冲编码;
if(支持编码){
决心({
路径
数据:buffer.toString(supportedEncoding),
});
}否则{
拒绝(新错误(“无法识别文件编码”);
}
但是,当chardet遇到一种在bufferencoding中没有明显模拟的编码时,什么是好的方法呢?像今天一样,我输入了iso-8859-2

但是,当chardet遇到一种在bufferencoding中没有明显模拟的编码时,什么是好的方法呢

在以下情况下,您需要编写解码器:

var iso88592=require('iso-8859-2')//https://www.npmjs.com/package/iso-8859-2
const chartdetofsencodings=新地图([
['UTF-8','utf8'],
['UTF-16LE','utf16le'],
['iso-8859-2',函数解码ISO88592(缓冲区){
返回iso88592.decode(buffer.toString('binary'))
}]
])
const plausableEncodings=analysis(buffer).map((match)=>match.name)
const supportedEncoding=plausableEncodings.find((名称)=>chartdetToFsEncodings.get(名称))
if(支持编码){
让数据
if(支持的类型编码==='function'){
数据=支持编码(缓冲区)
}否则{
数据=缓冲区.toString(supportedEncoding)
}
解析({path,data})
}否则{
拒绝(新错误(“文件编码未识别”))
}
通常,API接受UTF8,因为它管理所有字符,而
latin2
是UTF8的子集