Skip to content

Instantly share code, notes, and snippets.

@backsapce
Last active August 29, 2015 14:27
Show Gist options
  • Save backsapce/a7966379371783bfc709 to your computer and use it in GitHub Desktop.
Save backsapce/a7966379371783bfc709 to your computer and use it in GitHub Desktop.
achiver zip compress download comptible with ie 6 filefox chrome.NOTE: Only a example and can not run derectly
var fs = Npm.require('fs');
var path = Npm.require('path');
var archiver = Npm.require('archiver');
var req = this.request;
var res = this.response;
//create archiver class
var zip_name; //must with .zip ext. for example xxx.zip
var archive = archiver('zip', {
name: zip_name
});
var userAgent = (req.headers['user-agent'] || '').toLowerCase();
var contentDisposition;
if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
contentDisposition = 'attachment; filename=' + encodeURIComponent(zip_name);
} else if (userAgent.indexOf('firefox') >= 0) {
contentDisposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(zip_name) + '"';
} else {
/* safari and other browsers */
contentDisposition = 'attachment; filename=' + new Buffer(zip_name).toString('binary');
}
res.writeHeader(200, {
"Content-Type": "application/zip",
'Content-disposition': contentDisposition
});
res.on('end', function() {
return res.send('OK').end();
});
for (var i = 0; i < files.length; i++) {
var ab_path = path.join(files[i].save_path, files[i].gen_save_path, files[i].save_name);
if (fs.existsSync(ab_path)) {
var fstream = fs.createReadStream(ab_path);
//设定关闭流操作
fstream.on('close', function() {
this.close();
});
archive.append(fstream, {
name: files[i].file_name
});
} else {
return return_404(res);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment