Skip to content

Commit ccb1475

Browse files
committed
avoid using general purpose bit 3 when we can. closes #13
1 parent a1deb1e commit ccb1475

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

index.js

+22-5
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,10 @@ function Entry(metadataPath, isDirectory, options) {
257257
this.uncompressedSize = 0;
258258
this.compressedSize = 0;
259259
} else {
260-
this.uncompressedSize = null; // unknown
260+
// unknown
261+
this.crc32 = null;
262+
this.uncompressedSize = null;
263+
this.compressedSize = null;
261264
if (options.size != null) this.uncompressedSize = options.size;
262265
}
263266
if (isDirectory) {
@@ -294,17 +297,31 @@ var VERSION_MADE_BY_INFO_ZIP = 0x031e;
294297
var FILE_NAME_IS_UTF8 = 1 << 11;
295298
var UNKNOWN_CRC32_AND_FILE_SIZES = 1 << 3;
296299
Entry.prototype.getLocalFileHeader = function() {
300+
var crcAndFileSizeKnown = this.crc32 != null &&
301+
this.uncompressedSize != null &&
302+
this.compressedSize != null;
303+
var crc32 = 0;
304+
var compressedSize = 0;
305+
var uncompressedSize = 0;
306+
if (crcAndFileSizeKnown) {
307+
crc32 = this.crc32;
308+
compressedSize = this.compressedSize;
309+
uncompressedSize = this.uncompressedSize;
310+
}
311+
297312
var fixedSizeStuff = new Buffer(LOCAL_FILE_HEADER_FIXED_SIZE);
298-
var generalPurposeBitFlag = UNKNOWN_CRC32_AND_FILE_SIZES | FILE_NAME_IS_UTF8;
313+
var generalPurposeBitFlag = FILE_NAME_IS_UTF8;
314+
if (!crcAndFileSizeKnown) generalPurposeBitFlag |= UNKNOWN_CRC32_AND_FILE_SIZES;
315+
299316
fixedSizeStuff.writeUInt32LE(0x04034b50, 0); // local file header signature 4 bytes (0x04034b50)
300317
fixedSizeStuff.writeUInt16LE(VERSION_NEEDED_TO_EXTRACT, 4); // version needed to extract 2 bytes
301318
fixedSizeStuff.writeUInt16LE(generalPurposeBitFlag, 6); // general purpose bit flag 2 bytes
302319
fixedSizeStuff.writeUInt16LE(this.getCompressionMethod(), 8); // compression method 2 bytes
303320
fixedSizeStuff.writeUInt16LE(this.lastModFileTime, 10); // last mod file time 2 bytes
304321
fixedSizeStuff.writeUInt16LE(this.lastModFileDate, 12); // last mod file date 2 bytes
305-
fixedSizeStuff.writeUInt32LE(0, 14); // crc-32 4 bytes
306-
fixedSizeStuff.writeUInt32LE(0, 18); // compressed size 4 bytes
307-
fixedSizeStuff.writeUInt32LE(0, 22); // uncompressed size 4 bytes
322+
fixedSizeStuff.writeUInt32LE(crc32, 14); // crc-32 4 bytes
323+
fixedSizeStuff.writeUInt32LE(compressedSize, 18); // compressed size 4 bytes
324+
fixedSizeStuff.writeUInt32LE(uncompressedSize, 22); // uncompressed size 4 bytes
308325
fixedSizeStuff.writeUInt16LE(this.utf8FileName.length, 26); // file name length 2 bytes
309326
fixedSizeStuff.writeUInt16LE(0, 28); // extra field length 2 bytes
310327
return Buffer.concat([

0 commit comments

Comments
 (0)