node-unzipper 加密压缩包的解压

由 夕空 撰写于  2020年4月15日
首先npm下载需要的包
$ npm install unzipper

$ npm install iconv-lite

iconv-lite是为了正常显示中文字符,解决中文乱码


常规zip解压

function unZip(zipPath, extractPath) {
fs.createReadStream(zipPath)
.pipe(unzipper.Parse()) //.pipe(unzipper.Extract({ path: 'output/path' }));
.on('entry', function (entry) {console.log(entry)
const tmpFilePath = extractPath + "/" + iconv.decode(entry.props.pathBuffer, 'gbk');
const type = entry.type; // 'Directory' or 'File'
if (type === "File") {
entry.pipe(fs.createWriteStream(tmpFilePath));
} else if(type === "Directory" && !fs.existsSync(tmpFilePath)) {
fs.mkdirSync(tmpFilePath);
}
})
.promise().then(function (e) {
if (!e) {
console.log("解压成功");
}
})
}
unZip('./abc.zip', './temp');



加密zip解压(发现大文件超慢,不知啥问题):

function unZipmi(zipPath, extractPath, password) {
unzipper.Open.file(zipPath).then((d) => {
return new Promise((resolve, reject) => {
for (i = 0; i < d.files.length; i++) {
const file = d.files[i]; console.log(file)
const tmpFilePath = extractPath + "/" + iconv.decode(file.pathBuffer, 'gbk');
if (file.type == "File") {
d.files[i].stream(password)
.pipe(fs.createWriteStream(tmpFilePath))
.on('finish', () => resolve(tmpFilePath))
.on('error', reject);
} else if (file.type == "Directory" && !fs.existsSync(tmpFilePath)) {
fs.mkdirSync(tmpFilePath);
}

}

})
}).then((path) => {
console.log('file(s) written to ' + path);
}).catch((e) => {
console.log('error while processing archive', e);
});
}
unZipmi('./un.zip', './temp', "abc123");

声明:星耀夕空|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - node-unzipper 加密压缩包的解压


欢迎光顾我的小站!