nodejs用archiver模塊壓縮文件

router.get('/download/qiantu',function(req,res){
	var dirname = (new Date()).getTime()
	var zippath = './down/'+dirname+'.zip'
	
	var output = fs.createWriteStream(zippath)
	var ziparchiver = archiver('zip',{
		store:true
	})
	ziparchiver.pipe(output)
	async.mapSeries(req.session.qtimagesurl,function(item, callback){//異步下載
    	setTimeout(function(){
        	//downloadPic(item, dir+'/'+ (new Date()).getTime() +'.jpg');
        	ziparchiver.append(request(item),{'name':'images/'+(new Date()).getTime() +'.jpg'})
       		callback(null, item);
    	},400);
	}, function(err, results){
		req.session.qtimagesurl =[] //下載完成後清空
		ziparchiver.finalize()
		res.render('index')
	});
})

 request(item),請求遠程圖片文件用request,本地文件就用fs.createReadStreamsession

item爲圖片路徑app

var output = fs.createWriteStream(zippath)這個是壓縮文件名字異步

{'name':'images/'+(new Date()).getTime() +'.jpg'}
壓縮文件解壓後的命名,/前面會自動生產相應文件夾async

ziparchiver.finalize()結束操做url

相關文章
相關標籤/搜索