nodejs zip 解压zip文件

来源:互联网 发布:四川省广电网络官网 编辑:程序博客网 时间:2024/06/10 17:19

官方文档上写的教程是

var unzippedfs = zipper.sync.unzip(“pack.zip”).memory();
首先unzip是一个文件,写egg时浏览器传的流,可以转成buffer,但是文档上未写buffer的情况
源码分析main.js找unzip()

ZipLocal.unzip = function (file, _callback) {    var callback = _callback || function () { };    var zipped_obj = JSZip.make();    if (typeof file === "string") {        ////    }    else if (file instanceof Buffer) {        ////    }    else {        callback(new Error("Unsupported type: data is neither a path or a Buffer"));    }}

其实他是写了这个功能的,只是在文档上没说明,所以解压的使用方法是

                var unzippedfs = zipper.sync.unzip(buf).memory();                for (const fileName of unzippedfs.contents()) {                    var buff = unzippedfs.read(fileName, "buffer");                    let fileDeepPath = path.resolve(dir + '/' + fileName)                    fs.existsSync(path.dirname(fileDeepPath)) == false && this.ctx.app.mkdir(path.dirname(fileDeepPath));                    fs.writeFileSync(fileDeepPath, buff);                }
原创粉丝点击