nodejs 解压文件,当文件名为中文名时会出现乱码
一开始我是使用compressing 去解压的,后面一直出现乱码,于是换了个解压的包 yauzl
具体实现步骤:
安装
npm install yauzl
2. 封装了个解压文件方法
function unzip(zipFileName, outputDir){ const yauzl = require('yauzl') return new Promise((resolve, reject) => { if (!fs.existsSync(outputDir)) { fs.mkdirSync(outputDir, { recursive: true }); } yauzl.open(zipFileName, {lazyEntries: true}, function(err, zipfile) { if (err) reject(err); else { zipfile.readEntry(); zipfile.on("entry", function(entry) { if (/\/$/.test(entry.fileName)) { var dir = path.join(outputDir, entry.fileName); if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } zipfile.readEntry(); } else { zipfile.openReadStream(entry, function(err, readStream) { if (err) reject(err); else { var fileName = path.join(outputDir, entry.fileName); readStream.pipe(fs.createWriteStream(fileName)); readStream.on("end", function() { zipfile.readEntry(); }); } }); } }).on("close",function(){ resolve("解压完成"); }); } }); }); }
3. 调用
unzip(zipPath, outputPath).then(msg => console.log(msg)).catch(err => console.error(err)); // zipPath 是要解压的文件路径 outputPath是要输出的目录
使用以上方法即可解决乱码问题