diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index cd519ad4..ec808c7d 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -28,3 +28,8 @@ jobs: npm test env: CI: true + - name: Archive production artifacts + uses: actions/upload-artifact@v2 + with: + name: tmp-zip + path: tmp/*.zip diff --git a/index.js b/index.js index 053041b6..2c692376 100644 --- a/index.js +++ b/index.js @@ -42,6 +42,8 @@ var ZipStream = module.exports = function(options) { options.store = true; } + options.namePrependSlash = options.namePrependSlash || false; + if (options.comment && options.comment.length > 0) { this.setComment(options.comment); } @@ -60,6 +62,7 @@ ZipStream.prototype._normalizeFileData = function(data) { data = util.defaults(data, { type: 'file', name: null, + namePrependSlash: this.options.namePrependSlash, linkname: null, date: null, mode: null, @@ -130,6 +133,10 @@ ZipStream.prototype.entry = function(source, data, callback) { var entry = new ZipArchiveEntry(data.name); entry.setTime(data.date, this.options.forceLocalTime); + if (data.namePrependSlash) { + entry.setName(data.name, true); + } + if (data.store) { entry.setMethod(0); } diff --git a/package-lock.json b/package-lock.json index a20ddfce..80c135dc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -296,9 +296,9 @@ "dev": true }, "compress-commons": { - "version": "4.0.2", - "resolved": "https://registry.npmjs.org/compress-commons/-/compress-commons-4.0.2.tgz", - "integrity": "sha512-qhd32a9xgzmpfoga1VQEiLEwdKZ6Plnpx5UCgIsf89FSolyJ7WnifY4Gtjgv5WR6hWAyRaHxC5MiEhU/38U70A==", + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/compress-commons/-/compress-commons-4.1.0.tgz", + "integrity": "sha512-ofaaLqfraD1YRTkrRKPCrGJ1pFeDG/MVCkVVV2FNGeWquSlqw5wOrwOfPQ1xF2u+blpeWASie5EubHz+vsNIgA==", "requires": { "buffer-crc32": "^0.2.13", "crc32-stream": "^4.0.1", diff --git a/package.json b/package.json index a727fb9b..de5f992d 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ }, "dependencies": { "archiver-utils": "^2.1.0", - "compress-commons": "^4.0.2", + "compress-commons": "^4.1.0", "readable-stream": "^3.6.0" }, "devDependencies": { diff --git a/test/pack.js b/test/pack.js index 9f41b341..f7b9030b 100644 --- a/test/pack.js +++ b/test/pack.js @@ -331,6 +331,29 @@ describe('pack', function() { }); }); + + it('should support appending forward slash to entry names', function(done) { + var archive = new Packer({ + namePrependSlash: true, + }); + var testStream = fs.createWriteStream('tmp/name-prepend-slash.zip'); + + testStream.on('close', function() { + done(); + }); + + archive.pipe(testStream); + + archive.entry('some text', { name: 'file', namePrependSlash: false, date: testDate }, function(err) { + if (err) throw err; + archive.entry('more text', { type: 'file', name: 'file-with-prefix', date: testDate }, function(err) { + if (err) throw err; + archive.finalize(); + }); + }); + }); + + }); });