node,js download實現下載文件並解壓后刪除


 var express = require('express');
  var router = express.Router();
  const fs = require('fs');
  var unzip = require("unzip");
  const download = require('download');
(async () => {
    // await download('http://172.20.33.25:8088/upload/download.action?fileName=345.zip&md5=bc09d226db664c8b362743608f6f74ca&type=zip&size=153&fileId=1264&time=1599548153286&filename=bc09d226db664c8b362743608f6f74ca.zip ', './');

    fs.writeFileSync('./123.zip', await download('http://172.20.33.25:8088/upload/download.action?fileName=345.zip&md5=bc09d226db664c8b362743608f6f74ca&type=zip&size=153&fileId=1264&time=1599548153286&filename=bc09d226db664c8b362743608f6f74ca.zip'));

    // download('http://172.20.33.25:8088/upload/download.action?fileName=345.zip&md5=bc09d226db664c8b362743608f6f74ca&type=zip&size=153&fileId=1264&time=1599548153286&filename=bc09d226db664c8b362743608f6f74ca.zip ').pipe(fs.createWriteStream('./123.zip'));

    // await Promise.all([
    //     'http://172.20.33.25:8088/upload/download.action?fileName=345.zip&md5=bc09d226db664c8b362743608f6f74ca&type=zip&size=153&fileId=1264&time=1599548153286&filename=bc09d226db664c8b362743608f6f74ca.zip ',
    // ].map(url => download(url, './')));

    var extract = unzip.Extract({ path: './' });
    fs.createReadStream('./123.zip').pipe(extract);
    extract.on('close', function () {
        console.log("解壓完成!!");
        //刪除
        fs.unlinkSync('./123.zip');
    });
    extract.on('error', function (err) {
        console.log(err);
    });

  })();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM