基于nodejs+python的解压缩

0x01 技术栈


python.js 在github中有。
nodejs 自己安装就好了.

0x02 coding


/**
 * Author: Smarttang
 * Date: 2016.10.16
 *
 * ------
 *  基于python的zip解压
 *  可以请求post和get
 *  zip = require('./zipUtils')
 *  zip.init('/xxx/xxx.zip','/tmp/xxxx/')
 *  var status = zip.do()
 */
var python = require('python.js');
var zipfile = python.import('zipfile');


module.exports = {
    /**
     * 初始化配置
     * @param  {[type]} filename [需要解压的文件压缩包(.zip)]
     * @param  {[type]} dpath    [解压拼装的路径]
     * @return {[type]}          [description]
     */
    init: function(filename,dpath){
        this.filename = filename;
        this.dpath = dpath;
        // 判断是否有报告
        this.report = false;
    },
    /**
     * 解压缩报告文件
     * @return {[type]} [description]
     */
    do: function(){
        // 判断是不是压缩文件
        if(zipfile.is_zipfile(this.filename)){
            // 打开读取
            var fz = zipfile.ZipFile(this.filename,'r');
            // 将数据转换成列表数组
            var filelist = eval(JSON.parse(JSON.stringify(''+fz.namelist())));
            // 遍历找报告文件是否存在
            for (var i in filelist){
                if (filelist[i] == 'report.pdf'){
                    // 将报告释放到指定的目录下面
                    fz.extract('report.pdf',this.dpath);
                    this.report = true;
                }
            }
        }
        return this.report;
    }
};

你可能感兴趣的:(基于nodejs+python的解压缩)