Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for archive + file comments #44

Merged
merged 3 commits into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ File paths must not end with `"/"`.
mode: stats.mode,
compress: true,
forceZip64Format: false,
fileComment: ""
}
```

Expand Down Expand Up @@ -97,6 +98,7 @@ See `addFile()` for info about the `metadataPath` parameter.
mode: 0o100664,
compress: true,
forceZip64Format: false,
fileComment: "",
size: 12345, // example value
}
```
Expand All @@ -121,6 +123,7 @@ See `addFile()` for info about the `metadataPath` parameter.
mode: 0o100664,
compress: true,
forceZip64Format: false,
fileComment: ""
}
```

Expand Down Expand Up @@ -182,6 +185,7 @@ and causes the eventual close of `outputStream`.

```js
{
comment: "",
forceZip64Format: false,
}
```
Expand Down
18 changes: 10 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ ZipFile.prototype.end = function(options, finalSizeCallback) {
this.ended = true;
this.finalSizeCallback = finalSizeCallback;
this.forceZip64Eocd = !!options.forceZip64Format;
this.comment = Buffer.from(options.comment || "", "utf-8");
pumpEntries(this);
};

Expand Down Expand Up @@ -226,7 +227,7 @@ function calculateFinalSize(self) {
}
}

centralDirectorySize += CENTRAL_DIRECTORY_RECORD_FIXED_SIZE + entry.utf8FileName.length;
centralDirectorySize += CENTRAL_DIRECTORY_RECORD_FIXED_SIZE + entry.utf8FileName.length + entry.fileComment.length;
if (useZip64Format) {
centralDirectorySize += ZIP64_EXTENDED_INFORMATION_EXTRA_FIELD_SIZE;
}
Expand All @@ -240,7 +241,7 @@ function calculateFinalSize(self) {
// use zip64 end of central directory stuff
endOfCentralDirectorySize += ZIP64_END_OF_CENTRAL_DIRECTORY_RECORD_SIZE + ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_SIZE;
}
endOfCentralDirectorySize += END_OF_CENTRAL_DIRECTORY_RECORD_SIZE;
endOfCentralDirectorySize += END_OF_CENTRAL_DIRECTORY_RECORD_SIZE + self.comment.length;
return pretendOutputCursor + centralDirectorySize + endOfCentralDirectorySize;
}

Expand Down Expand Up @@ -277,7 +278,7 @@ function getEndOfCentralDirectoryRecord(self, actuallyJustTellMeHowLongItWouldBe
}
}

var eocdrBuffer = Buffer.allocUnsafe(END_OF_CENTRAL_DIRECTORY_RECORD_SIZE);
var eocdrBuffer = Buffer.allocUnsafe(END_OF_CENTRAL_DIRECTORY_RECORD_SIZE + self.comment.length);
// end of central dir signature 4 bytes (0x06054b50)
eocdrBuffer.writeUInt32LE(0x06054b50, 0);
// number of this disk 2 bytes
Expand All @@ -293,9 +294,9 @@ function getEndOfCentralDirectoryRecord(self, actuallyJustTellMeHowLongItWouldBe
// offset of start of central directory with respect to the starting disk number 4 bytes
eocdrBuffer.writeUInt32LE(normalOffsetOfStartOfCentralDirectory, 16);
// .ZIP file comment length 2 bytes
eocdrBuffer.writeUInt16LE(0, 20);
eocdrBuffer.writeUInt16LE(self.comment.length, 20);
// .ZIP file comment (variable size)
// no comment
self.comment.copy(eocdrBuffer, 22);

if (!needZip64Format) return eocdrBuffer;

Expand Down Expand Up @@ -392,6 +393,7 @@ function Entry(metadataPath, isDirectory, options) {
if (options.compress != null) this.compress = !!options.compress;
}
this.forceZip64Format = !!options.forceZip64Format;
this.fileComment = Buffer.from(options.fileComment || "", "utf-8");
}
Entry.WAITING_FOR_METADATA = 0;
Entry.READY_TO_PUMP_FILE_DATA = 1;
Expand Down Expand Up @@ -565,7 +567,7 @@ Entry.prototype.getCentralDirectoryRecord = function() {
// extra field length 2 bytes
fixedSizeStuff.writeUInt16LE(zeiefBuffer.length, 30);
// file comment length 2 bytes
fixedSizeStuff.writeUInt16LE(0, 32);
fixedSizeStuff.writeUInt16LE(this.fileComment.length, 32);
// disk number start 2 bytes
fixedSizeStuff.writeUInt16LE(0, 34);
// internal file attributes 2 bytes
Expand All @@ -582,7 +584,7 @@ Entry.prototype.getCentralDirectoryRecord = function() {
// extra field (variable size)
zeiefBuffer,
// file comment (variable size)
// empty comment
this.fileComment
]);
};
Entry.prototype.getCompressionMethod = function() {
Expand All @@ -606,7 +608,7 @@ function dateToDosDateTime(jsDate) {
}

function writeUInt64LE(buffer, n, offset) {
// can't use bitshift here, because JavaScript only allows bitshiting on 32-bit integers.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💩💩💩

// can't use bitshift here, because JavaScript only allows bitshifting on 32-bit integers.
var high = Math.floor(n / 0x100000000);
var low = n % 0x100000000;
buffer.writeUInt32LE(low, offset);
Expand Down
48 changes: 48 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,51 @@ var BufferList = require("bl");
}));
});
})();

(function() {
var zipfile = new yazl.ZipFile();
var comment = "Hello World";
zipfile.end({
comment: comment
}, function(finalSize) {
if (finalSize === -1) throw new Error("finalSize should be known");
zipfile.outputStream.pipe(new BufferList(function(err, data) {
if (err) throw err;
if (data.length !== finalSize) throw new Error("finalSize prediction is wrong. " + finalSize + " !== " + data.length);
yauzl.fromBuffer(data, function(err, zipfile) {
if (err) throw err;
if (zipfile.comment !== comment) {
throw new Error("fileComment didn't match: " + zipfile.fileComment + ", expected: " + fileComment);
}
console.log("comment: pass");
});
}));
});
})();

(function() {
var fileComment = "Hello World";
var zipfile = new yazl.ZipFile();
// all options parameters are optional
zipfile.addBuffer(Buffer.from("hello"), "hello.txt", {compress: false, fileComment: fileComment});
zipfile.end(function(finalSize) {
if (finalSize === -1) throw new Error("finalSize should be known");
zipfile.outputStream.pipe(new BufferList(function(err, data) {
if (err) throw err;
if (data.length !== finalSize) throw new Error("finalSize prediction is wrong. " + finalSize + " !== " + data.length);
yauzl.fromBuffer(data, function(err, zipfile) {
if (err) throw err;
var entryNames = ["hello.txt"];
zipfile.on("entry", function(entry) {
var expectedName = entryNames.shift();
if (entry.fileComment !== fileComment) {
throw new Error("unexpected entry fileComment: " + entry.fileComment + ", expected: " + fileComment);
}
});
zipfile.on("end", function() {
if (entryNames.length === 0) console.log("fileComment: pass");
});
});
}));
});
})();