signalk-server
Version:
An implementation of a [Signal K](http://signalk.org) server for boats.
42 lines • 1.5 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendZip = sendZip;
const archiver_1 = __importDefault(require("archiver"));
const fs_1 = __importDefault(require("fs"));
/**
* Send a zip file as a download response.
* Replacement for express-easy-zip using archiver directly.
*
* Note: `filename` is used verbatim in the Content-Disposition header, so the
* caller is responsible for including any desired extension. This function
* intentionally does not append `.zip`.
*/
function sendZip(res, options) {
const { files, filename } = options;
res.setHeader('Content-Type', 'application/zip');
res.setHeader('Content-Disposition', `attachment; filename="${filename}"`);
const archive = (0, archiver_1.default)('zip', {
zlib: { level: 9 }
});
archive.on('error', (err) => {
console.error('Zip archive error:', err);
if (!res.headersSent) {
res.status(500).send('Error creating zip file');
}
});
archive.pipe(res);
for (const file of files) {
const stat = fs_1.default.statSync(file.path);
if (stat.isDirectory()) {
archive.directory(file.path, file.name);
}
else {
archive.file(file.path, { name: file.name });
}
}
archive.finalize();
}
//# sourceMappingURL=zip.js.map