Skip to content

Commit e8c32f8

Browse files
committed
Set comment in end() options
1 parent 0cf7706 commit e8c32f8

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ and causes the eventual close of `outputStream`.
191191

192192
```js
193193
{
194+
comment: "",
194195
forceZip64Format: false,
195196
}
196197
```

index.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ function ZipFile() {
1717
this.ended = false; // .end() sets this
1818
this.allDone = false; // set when we've written the last bytes
1919
this.forceZip64Eocd = false; // configurable in .end()
20-
this.comment = "";
2120
}
2221

2322
ZipFile.prototype.addFile = function(realPath, metadataPath, options) {
@@ -119,6 +118,7 @@ ZipFile.prototype.end = function(options, finalSizeCallback) {
119118
this.ended = true;
120119
this.finalSizeCallback = finalSizeCallback;
121120
this.forceZip64Eocd = !!options.forceZip64Format;
121+
this.comment = Buffer.from(options.comment || "", "utf-8");
122122
pumpEntries(this);
123123
};
124124

@@ -241,7 +241,7 @@ function calculateFinalSize(self) {
241241
// use zip64 end of central directory stuff
242242
endOfCentralDirectorySize += ZIP64_END_OF_CENTRAL_DIRECTORY_RECORD_SIZE + ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_SIZE;
243243
}
244-
endOfCentralDirectorySize += END_OF_CENTRAL_DIRECTORY_RECORD_SIZE + Buffer.byteLength(self.comment, "utf-8");
244+
endOfCentralDirectorySize += END_OF_CENTRAL_DIRECTORY_RECORD_SIZE + self.comment.length;
245245
return pretendOutputCursor + centralDirectorySize + endOfCentralDirectorySize;
246246
}
247247

@@ -278,8 +278,7 @@ function getEndOfCentralDirectoryRecord(self, actuallyJustTellMeHowLongItWouldBe
278278
}
279279
}
280280

281-
var comment = Buffer.from(self.comment, "utf-8")
282-
var eocdrBuffer = Buffer.alloc(END_OF_CENTRAL_DIRECTORY_RECORD_SIZE + comment.length);
281+
var eocdrBuffer = Buffer.alloc(END_OF_CENTRAL_DIRECTORY_RECORD_SIZE + self.comment.length);
283282
// end of central dir signature 4 bytes (0x06054b50)
284283
eocdrBuffer.writeUInt32LE(0x06054b50, 0);
285284
// number of this disk 2 bytes
@@ -295,9 +294,9 @@ function getEndOfCentralDirectoryRecord(self, actuallyJustTellMeHowLongItWouldBe
295294
// offset of start of central directory with respect to the starting disk number 4 bytes
296295
eocdrBuffer.writeUInt32LE(normalOffsetOfStartOfCentralDirectory, 16);
297296
// .ZIP file comment length 2 bytes
298-
eocdrBuffer.writeUInt16LE(comment.length, 20);
297+
eocdrBuffer.writeUInt16LE(self.comment.length, 20);
299298
// .ZIP file comment (variable size)
300-
comment.copy(eocdrBuffer, 22);
299+
self.comment.copy(eocdrBuffer, 22);
301300

302301
if (!needZip64Format) return eocdrBuffer;
303302

test/test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,9 @@ var BufferList = require("bl");
130130
(function() {
131131
var zipfile = new yazl.ZipFile();
132132
var comment = "Hello World";
133-
zipfile.comment = comment;
134-
zipfile.end(function(finalSize) {
133+
zipfile.end({
134+
comment: comment
135+
}, function(finalSize) {
135136
if (finalSize === -1) throw new Error("finalSize should be known");
136137
zipfile.outputStream.pipe(new BufferList(function(err, data) {
137138
if (err) throw err;

0 commit comments

Comments
 (0)