美文网首页Nodejs
nodejs模块之文件输出md5戳

nodejs模块之文件输出md5戳

作者: 蘇上方 | 来源:发表于2017-03-20 16:12 被阅读23次

    crypto是nodejs自带模块,无需安装,直接使用即可。
    以下代码执行完会计算出hash值并输出 config.json 文件,

    const crypto = require('crypto');
    const fs = require('fs');
    
    let computedHex = () => {
        let rs = fs.createReadStream('./' + zipName);
        let hash = crypto.createHash('md5');
        let hex
    
        rs.on('data', hash.update.bind(hash));
    
        rs.on('end', function() {
            hex = hash.digest('hex')
            console.log('hex is ' + hex);
            output(hex);
        });
    }
    
    let output = (hex) => {
        let arr = [];
        let json = {
            "name": "flyme.5.0_uc_h5.zip",
            "md5": hex,
            "lastmodify": new Date().getTime()
        };
        arr.push(json);
        fs.writeFile('./config.json', JSON.stringify(arr), function(err) {
            if (err) throw err;
            console.log('saved config.json successfully');
        });
    }
    
    zipper.zip("output/release", function(error, zipped) {
    
        if (!error) {
            zipped.compress(); // compress before exporting
    
            var buff = zipped.memory(); // get the zipped file as a Buffer
            // or save the zipped file to disk
            zipped.save("./" + zipName, function(error) {
                if (!error) {
                    console.log("Ziped files successfully !");
                    computedHex();
                }
            });
        } else {
            console.log(error)
        }
    });
    

    相关文章

      网友评论

        本文标题:nodejs模块之文件输出md5戳

        本文链接:https://www.haomeiwen.com/subject/qdcinttx.html