From 16464c4600c590e82c8a2feee13c87a44079ac0f Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Sun, 15 Nov 2026 04:21:04 +0530 Subject: [PATCH] fix: keep empty directories in compressDir --- lib/tar/stream.js | 8 ++++++++ lib/zip/stream.js | 4 ++++ test/tar/index.test.js | 12 ++++++++++++ test/zip/index.test.js | 12 ++++++++++++ 4 files changed, 36 insertions(+) diff --git a/lib/tar/stream.js b/lib/tar/stream.js index 6e64625..71a8e62 100644 --- a/lib/tar/stream.js +++ b/lib/tar/stream.js @@ -72,6 +72,10 @@ class TarStream extends BaseStream { if (err) return this.emit('error', err); const relativePath = opts.relativePath || ''; + if (files.length === 0) { + const dirName = opts.ignoreBase ? relativePath : path.posix.join(relativePath, path.basename(entry)); + if (dirName) this._addEmptyDirEntry(dirName); + } files.forEach(fileOrDir => { const newOpts = utils.clone(opts); if (opts.ignoreBase) { @@ -86,6 +90,10 @@ class TarStream extends BaseStream { }); } + _addEmptyDirEntry(name) { + this._pack.entry({ name: name + '/', type: 'directory' }); + } + _addBufferEntry(entry, opts) { if (!opts.relativePath) return this.emit('error', 'opts.relativePath is required if entry is a buffer'); this._pack.entry({ name: opts.relativePath }, entry, this._onEntryFinish.bind(this)); diff --git a/lib/zip/stream.js b/lib/zip/stream.js index fbbd2c2..303217e 100644 --- a/lib/zip/stream.js +++ b/lib/zip/stream.js @@ -18,6 +18,10 @@ class ZipStream extends TarStream { this._onEntryFinish(); } + _addEmptyDirEntry(name) { + this._zipfile.addEmptyDirectory(name); + } + _addBufferEntry(entry, opts) { if (!opts.relativePath) return this.emit('error', new Error('opts.relativePath is required if entry is a buffer')); this._zipfile.addBuffer(entry, opts.relativePath, opts); diff --git a/test/tar/index.test.js b/test/tar/index.test.js index 519a09f..946c031 100644 --- a/test/tar/index.test.js +++ b/test/tar/index.test.js @@ -117,6 +117,18 @@ describe('test/tar/index.test.js', () => { }); describe('tar.compressDir()', () => { + it('tar.compressDir(dir, destFile) keeps empty directories', async () => { + const sourceDir = path.join(os.tmpdir(), randomUUID()); + fs.mkdirSync(path.join(sourceDir, 'logs'), { recursive: true }); + fs.writeFileSync(path.join(sourceDir, 'a.txt'), 'hi'); + const destFile = path.join(os.tmpdir(), randomUUID() + '.tar'); + await compressing.tar.compressDir(sourceDir, destFile); + + const destDir = path.join(os.tmpdir(), randomUUID()); + await compressing.tar.uncompress(destFile, destDir); + assert(fs.statSync(path.join(destDir, path.basename(sourceDir), 'logs')).isDirectory()); + }); + it('tar.compressDir(dir, destFile)', async () => { const sourceDir = path.join(__dirname, '..', 'fixtures'); const destFile = path.join(os.tmpdir(), randomUUID() + '.tar'); diff --git a/test/zip/index.test.js b/test/zip/index.test.js index 8434870..4d38b63 100644 --- a/test/zip/index.test.js +++ b/test/zip/index.test.js @@ -90,6 +90,18 @@ describe('test/zip/index.test.js', () => { }); describe('zip.compressDir()', () => { + it('zip.compressDir(dir, destFile) keeps empty directories', async () => { + const sourceDir = path.join(os.tmpdir(), randomUUID()); + fs.mkdirSync(path.join(sourceDir, 'logs'), { recursive: true }); + fs.writeFileSync(path.join(sourceDir, 'a.txt'), 'hi'); + const destFile = path.join(os.tmpdir(), randomUUID() + '.zip'); + await compressing.zip.compressDir(sourceDir, destFile); + + const destDir = path.join(os.tmpdir(), randomUUID()); + await compressing.zip.uncompress(destFile, destDir); + assert(fs.statSync(path.join(destDir, path.basename(sourceDir), 'logs')).isDirectory()); + }); + it('zip.compressDir(dir, destFile)', async () => { const sourceDir = path.join(__dirname, '..', 'fixtures'); destDir = path.join(os.tmpdir(), randomUUID());