Support custom entry names and abort on disconnect in zipDirectoriesPipe

This commit is contained in:
mikiher committed 2026-12-03 05:29:22 +02:00
1 parent 938fd36b1d
commit 5ba7a3fee5
1 file changed
+9 -5
+9 -5
View File
@@ -54,9 +54,9 @@ module.exports.zipDirectoryPipe = (path, filename, res) => {
}
/**
* Creates a zip archive containing multiple directories and streams it to the response.
* Creates a zip archive containing multiple directories or files and streams it to the response.
*
* @param {{ path: string, isFile: boolean }[]} pathObjects
* @param {{ path: string, isFile: boolean, name?: string }[]} pathObjects
* @param {string} filename - Name of the zip file to be sent as attachment.
* @param {Response} res - Response object to pipe the archive data to.
* @returns {Promise<void>} - Promise that resolves when the zip operation completes.
@@ -73,6 +73,9 @@ module.exports.zipDirectoriesPipe = (pathObjects, filename, res) => {
// listen for all archive data to be written
// 'close' event is fired only when a file descriptor is involved
res.on('close', () => {
if (!res.writableEnded) {
archive.abort()
}
Logger.info(archive.pointer() + ' total bytes')
Logger.debug('archiver has been finalized and the output file descriptor has closed.')
resolve()
@@ -105,13 +108,14 @@ module.exports.zipDirectoriesPipe = (pathObjects, filename, res) => {
// pipe archive data to the file
archive.pipe(res)
// Add each path as a directory in the zip
// Add each path as a directory or file in the zip
pathObjects.forEach((pathObject) => {
const entryName = pathObject.name || Path.basename(pathObject.path)
if (!pathObject.isFile) {
// Add the directory to the archive with its name as the root folder
archive.directory(pathObject.path, Path.basename(pathObject.path))
archive.directory(pathObject.path, entryName)
} else {
archive.file(pathObject.path, { name: Path.basename(pathObject.path) })
archive.file(pathObject.path, { name: entryName })
}
})