express adm-zip

Reference:
https://www.npmjs.com/package/adm-zip

npm i adm-zip

var AdmZip = require('adm-zip');


zip:

var zip = new AdmZip();
app.get('/generateZip',function(req,res){
	var content = "inner content of the file"; 
	zip.addFile('zipdemo.txt',Buffer.alloc(content.length,content),"comment");
	zip.addLocalFile("F:/express/pd/uploads/0e7c7ba77f991465157d14c1c0d0322b");
	// var willSendthis = zip.toBuffer();
	// console.log(willSendthis);  buffer stream data
	zip.writeZip('F:/express/pd/uploads/zip2.zip'); absolute path !!!
	res.json({code:1});
});

unzip:

app.get("/extractZip",function(req,res){
	var zip = new AdmZip("F:/express/pd/uploads/zip1.zip");
	var zipEntries = zip.getEntries();
	zipEntries.forEach(function(zipEntry){
		console.log(zipEntry.toString(),123);
		if(zipEntry.entryName =="zipdemo.txt"){
			console.log(zipEntry.getData().toString('utf8'),456);
		}
	});
	console.log(zip.readAsText("F:/express/pd/text.txt"),789);
	// zip.extractEntryTo("F:/express/pd/zip1.zip","F:/express/pd/uploads/",false,true);   error://enrty is emrty????
	zip.extractAllTo("F:/express/pd/uploads/",true);
});

posted @ 2018-11-26 19:31  cyany_blue  阅读(339)  评论(0编辑  收藏  举报