Skip to content

Commit

Permalink
Allow prepending forward slash in entry name (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctalkington authored Mar 3, 2021
1 parent 8d5bb11 commit 74646e3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
23 changes: 23 additions & 0 deletions test/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});


});

});

0 comments on commit 74646e3

Please sign in to comment.