@zip.js/zip.js 2.6.59 → 2.6.61

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8535,12 +8535,9 @@
8535
8535
  const commentOffset = endOfDirectoryInfo.offset;
8536
8536
  const commentLength = getUint16(endOfDirectoryView, 20);
8537
8537
  const appendedDataOffset = commentOffset + END_OF_CENTRAL_DIR_LENGTH + commentLength;
8538
- const lastDiskNumber = getUint16(endOfDirectoryView, 4);
8538
+ let lastDiskNumber = getUint16(endOfDirectoryView, 4);
8539
8539
  const expectedLastDiskNumber = reader.lastDiskNumber || 0;
8540
8540
  let diskNumber = getUint16(endOfDirectoryView, 6);
8541
- if (expectedLastDiskNumber != lastDiskNumber) {
8542
- throw new Error(ERR_SPLIT_ZIP_FILE);
8543
- }
8544
8541
  let filesLength = getUint16(endOfDirectoryView, 8);
8545
8542
  let prependedDataLength = 0;
8546
8543
  let startOffset = 0;
@@ -8564,8 +8561,11 @@
8564
8561
  if (getUint32(endOfDirectoryView, 0) != ZIP64_END_OF_CENTRAL_DIR_SIGNATURE) {
8565
8562
  throw new Error(ERR_EOCDR_LOCATOR_ZIP64_NOT_FOUND);
8566
8563
  }
8564
+ if (lastDiskNumber == MAX_16_BITS) {
8565
+ lastDiskNumber = getUint32(endOfDirectoryView, 16);
8566
+ }
8567
8567
  if (diskNumber == MAX_16_BITS) {
8568
- diskNumber = getUint32(endOfDirectoryView, 16);
8568
+ diskNumber = getUint32(endOfDirectoryView, 20);
8569
8569
  }
8570
8570
  if (filesLength == MAX_16_BITS) {
8571
8571
  filesLength = getBigUint64(endOfDirectoryView, 32);
@@ -8575,6 +8575,9 @@
8575
8575
  }
8576
8576
  directoryDataOffset -= directoryDataLength;
8577
8577
  }
8578
+ if (expectedLastDiskNumber != lastDiskNumber) {
8579
+ throw new Error(ERR_SPLIT_ZIP_FILE);
8580
+ }
8578
8581
  if (directoryDataOffset < 0 || directoryDataOffset >= reader.size) {
8579
8582
  throw new Error(ERR_BAD_FORMAT);
8580
8583
  }
@@ -9071,7 +9074,6 @@
9071
9074
  const ERR_UNSUPPORTED_FORMAT = "Zip64 is not supported (make sure 'keepOrder' is set to 'true')";
9072
9075
 
9073
9076
  const EXTRAFIELD_DATA_AES = new Uint8Array([0x07, 0x00, 0x02, 0x00, 0x41, 0x45, 0x03, 0x00, 0x00]);
9074
- const EXTRAFIELD_LENGTH_ZIP64 = 28;
9075
9077
 
9076
9078
  let workers = 0;
9077
9079
  const pendingEntries = [];
@@ -9214,6 +9216,7 @@
9214
9216
  let maximumCompressedSize = 0;
9215
9217
  let maximumEntrySize = 0;
9216
9218
  let uncompressedSize = 0;
9219
+ const zip64Enabled = zip64 === true;
9217
9220
  if (reader) {
9218
9221
  reader = initReader(reader);
9219
9222
  await initStream(reader);
@@ -9229,10 +9232,12 @@
9229
9232
  }
9230
9233
  }
9231
9234
  const { diskOffset, diskNumber, maxSize } = zipWriter.writer;
9232
- if (zipWriter.offset + zipWriter.pendingEntriesSize - diskOffset >= MAX_32_BITS ||
9233
- uncompressedSize >= MAX_32_BITS ||
9234
- maximumCompressedSize >= MAX_32_BITS ||
9235
- diskNumber + Math.ceil(zipWriter.pendingEntriesSize / maxSize) >= MAX_16_BITS) {
9235
+ const zip64UncompressedSize = zip64Enabled || uncompressedSize >= MAX_32_BITS;
9236
+ const zip64CompressedSize = zip64Enabled || maximumCompressedSize >= MAX_32_BITS;
9237
+ const zip64Offset = zip64Enabled || zipWriter.offset + zipWriter.pendingEntriesSize - diskOffset >= MAX_32_BITS;
9238
+ const supportZip64SplitFile = getOptionValue(zipWriter, options, "supportZip64SplitFile", true);
9239
+ const zip64DiskNumberStart = (supportZip64SplitFile && zip64Enabled) || diskNumber + Math.ceil(zipWriter.pendingEntriesSize / maxSize) >= MAX_16_BITS;
9240
+ if (zip64Offset || zip64UncompressedSize || zip64CompressedSize || zip64DiskNumberStart) {
9236
9241
  if (zip64 === false || !keepOrder) {
9237
9242
  throw new Error(ERR_UNSUPPORTED_FORMAT);
9238
9243
  } else {
@@ -9250,6 +9255,10 @@
9250
9255
  creationDate,
9251
9256
  rawExtraField,
9252
9257
  zip64,
9258
+ zip64UncompressedSize,
9259
+ zip64CompressedSize,
9260
+ zip64Offset,
9261
+ zip64DiskNumberStart,
9253
9262
  password,
9254
9263
  level,
9255
9264
  useWebWorkers,
@@ -9308,7 +9317,7 @@
9308
9317
  lockPreviousFileEntry = previousFileEntry && previousFileEntry.lock;
9309
9318
  requestLockCurrentFileEntry();
9310
9319
  }
9311
- if (options.bufferedWrite || zipWriter.writerLocked || zipWriter.bufferedWrites || !dataDescriptor) {
9320
+ if (options.bufferedWrite || zipWriter.writerLocked || (zipWriter.bufferedWrites && keepOrder) || !dataDescriptor) {
9312
9321
  fileWriter = new BlobWriter();
9313
9322
  fileWriter.writable.size = 0;
9314
9323
  bufferedWrite = true;
@@ -9316,7 +9325,7 @@
9316
9325
  await initStream(writer);
9317
9326
  } else {
9318
9327
  fileWriter = writer;
9319
- requestLockWriter();
9328
+ await requestLockWriter();
9320
9329
  }
9321
9330
  await initStream(fileWriter);
9322
9331
  const { writable } = writer;
@@ -9347,7 +9356,7 @@
9347
9356
  await requestLockWriter();
9348
9357
  writingBufferedEntryData = true;
9349
9358
  if (!dataDescriptor) {
9350
- blob = await writeMissingHeaderInfo(fileEntry, blob, writable, options);
9359
+ blob = await writeExtraHeaderInfo(fileEntry, blob, writable, options);
9351
9360
  }
9352
9361
  await skipDiskIfNeeded(writable);
9353
9362
  fileEntry.diskNumberStart = writer.diskNumber;
@@ -9358,7 +9367,7 @@
9358
9367
  }
9359
9368
  fileEntry.offset = zipWriter.offset - diskOffset;
9360
9369
  if (fileEntry.zip64) {
9361
- setZip64OffsetInfo(fileEntry);
9370
+ setZip64ExtraInfo(fileEntry, options);
9362
9371
  } else if (fileEntry.offset >= MAX_32_BITS) {
9363
9372
  throw new Error(ERR_UNSUPPORTED_FORMAT);
9364
9373
  }
@@ -9396,7 +9405,7 @@
9396
9405
 
9397
9406
  async function requestLockWriter() {
9398
9407
  zipWriter.writerLocked = true;
9399
- const { lockWriter } = zipWriter;
9408
+ const { lockWriter } = zipWriter;
9400
9409
  zipWriter.lockWriter = new Promise(resolve => releaseLockWriter = () => {
9401
9410
  zipWriter.writerLocked = false;
9402
9411
  resolve();
@@ -9438,6 +9447,10 @@
9438
9447
  password,
9439
9448
  level,
9440
9449
  zip64,
9450
+ zip64UncompressedSize,
9451
+ zip64CompressedSize,
9452
+ zip64Offset,
9453
+ zip64DiskNumberStart,
9441
9454
  zipCrypto,
9442
9455
  dataDescriptor,
9443
9456
  directory,
@@ -9510,7 +9523,25 @@
9510
9523
  } else {
9511
9524
  await writeData(writable, localHeaderArray);
9512
9525
  }
9513
- const rawExtraFieldZip64 = zip64 ? new Uint8Array(EXTRAFIELD_LENGTH_ZIP64 + 4) : new Uint8Array();
9526
+ let rawExtraFieldZip64;
9527
+ if (zip64) {
9528
+ let rawExtraFieldZip64Length = 4;
9529
+ if (zip64UncompressedSize) {
9530
+ rawExtraFieldZip64Length += 8;
9531
+ }
9532
+ if (zip64CompressedSize) {
9533
+ rawExtraFieldZip64Length += 8;
9534
+ }
9535
+ if (zip64Offset) {
9536
+ rawExtraFieldZip64Length += 8;
9537
+ }
9538
+ if (zip64DiskNumberStart) {
9539
+ rawExtraFieldZip64Length += 4;
9540
+ }
9541
+ rawExtraFieldZip64 = new Uint8Array(rawExtraFieldZip64Length);
9542
+ } else {
9543
+ rawExtraFieldZip64 = new Uint8Array();
9544
+ }
9514
9545
  if (reader) {
9515
9546
  setEntryInfo({
9516
9547
  signature,
@@ -9525,6 +9556,7 @@
9525
9556
  await writeData(writable, dataDescriptorArray);
9526
9557
  }
9527
9558
  Object.assign(fileEntry, {
9559
+ uncompressedSize,
9528
9560
  compressedSize,
9529
9561
  lastModDate,
9530
9562
  rawLastModDate,
@@ -9536,7 +9568,11 @@
9536
9568
  version,
9537
9569
  headerArray,
9538
9570
  signature,
9539
- rawExtraFieldZip64
9571
+ rawExtraFieldZip64,
9572
+ zip64UncompressedSize,
9573
+ zip64CompressedSize,
9574
+ zip64Offset,
9575
+ zip64DiskNumberStart
9540
9576
  });
9541
9577
  return fileEntry;
9542
9578
  }
@@ -9713,6 +9749,8 @@
9713
9749
  } = dataDescriptorInfo;
9714
9750
  const {
9715
9751
  zip64,
9752
+ zip64UncompressedSize,
9753
+ zip64CompressedSize,
9716
9754
  zipCrypto,
9717
9755
  dataDescriptor
9718
9756
  } = options;
@@ -9725,11 +9763,17 @@
9725
9763
  if (zip64) {
9726
9764
  const rawExtraFieldZip64View = getDataView(rawExtraFieldZip64);
9727
9765
  setUint16(rawExtraFieldZip64View, 0, EXTRAFIELD_TYPE_ZIP64);
9728
- setUint16(rawExtraFieldZip64View, 2, EXTRAFIELD_LENGTH_ZIP64);
9729
- setUint32(headerView, 14, MAX_32_BITS);
9730
- setBigUint64(rawExtraFieldZip64View, 12, BigInt(compressedSize));
9731
- setUint32(headerView, 18, MAX_32_BITS);
9732
- setBigUint64(rawExtraFieldZip64View, 4, BigInt(uncompressedSize));
9766
+ setUint16(rawExtraFieldZip64View, 2, rawExtraFieldZip64.length - 4);
9767
+ let rawExtraFieldZip64Offset = 4;
9768
+ if (zip64UncompressedSize) {
9769
+ setUint32(headerView, 18, MAX_32_BITS);
9770
+ setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(uncompressedSize));
9771
+ rawExtraFieldZip64Offset += 8;
9772
+ }
9773
+ if (zip64CompressedSize) {
9774
+ setUint32(headerView, 14, MAX_32_BITS);
9775
+ setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(compressedSize));
9776
+ }
9733
9777
  if (dataDescriptor) {
9734
9778
  setBigUint64(dataDescriptorView, dataDescriptorOffset + 4, BigInt(compressedSize));
9735
9779
  setBigUint64(dataDescriptorView, dataDescriptorOffset + 12, BigInt(uncompressedSize));
@@ -9744,7 +9788,7 @@
9744
9788
  }
9745
9789
  }
9746
9790
 
9747
- async function writeMissingHeaderInfo(fileEntry, entryData, writable, { zipCrypto }) {
9791
+ async function writeExtraHeaderInfo(fileEntry, entryData, writable, { zipCrypto }) {
9748
9792
  const arrayBuffer = await sliceAsArrayBuffer(entryData, 0, 26);
9749
9793
  const arrayBufferView = new DataView(arrayBuffer);
9750
9794
  if (!fileEntry.encrypted || zipCrypto) {
@@ -9761,11 +9805,24 @@
9761
9805
  return entryData.slice(arrayBuffer.byteLength);
9762
9806
  }
9763
9807
 
9764
- function setZip64OffsetInfo(fileEntry) {
9808
+ function setZip64ExtraInfo(fileEntry, options) {
9765
9809
  const { rawExtraFieldZip64, offset, diskNumberStart } = fileEntry;
9810
+ const { zip64UncompressedSize, zip64CompressedSize, zip64Offset, zip64DiskNumberStart } = options;
9766
9811
  const rawExtraFieldZip64View = getDataView(rawExtraFieldZip64);
9767
- setBigUint64(rawExtraFieldZip64View, 20, BigInt(offset));
9768
- setUint32(rawExtraFieldZip64View, 28, diskNumberStart);
9812
+ let rawExtraFieldZip64Offset = 4;
9813
+ if (zip64UncompressedSize) {
9814
+ rawExtraFieldZip64Offset += 8;
9815
+ }
9816
+ if (zip64CompressedSize) {
9817
+ rawExtraFieldZip64Offset += 8;
9818
+ }
9819
+ if (zip64Offset) {
9820
+ setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(offset));
9821
+ rawExtraFieldZip64Offset += 8;
9822
+ }
9823
+ if (zip64DiskNumberStart) {
9824
+ setUint32(rawExtraFieldZip64View, rawExtraFieldZip64Offset, diskNumberStart);
9825
+ }
9769
9826
  }
9770
9827
 
9771
9828
  async function closeFile(zipWriter, comment, options) {
@@ -9812,12 +9869,18 @@
9812
9869
  headerArray,
9813
9870
  directory,
9814
9871
  zip64,
9872
+ zip64UncompressedSize,
9873
+ zip64CompressedSize,
9874
+ zip64DiskNumberStart,
9875
+ zip64Offset,
9815
9876
  msDosCompatible,
9816
9877
  internalFileAttribute,
9817
9878
  externalFileAttribute,
9818
9879
  extendedTimestamp,
9819
9880
  lastModDate,
9820
- diskNumberStart
9881
+ diskNumberStart,
9882
+ uncompressedSize,
9883
+ compressedSize
9821
9884
  } = fileEntry;
9822
9885
  let rawExtraFieldExtendedTimestamp;
9823
9886
  if (extendedTimestamp) {
@@ -9833,17 +9896,24 @@
9833
9896
  const extraFieldLength = getLength(rawExtraFieldZip64, rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS, rawExtraField);
9834
9897
  setUint32(directoryView, offset, CENTRAL_FILE_HEADER_SIGNATURE);
9835
9898
  setUint16(directoryView, offset + 4, versionMadeBy);
9899
+ const headerView = getDataView(headerArray);
9900
+ if (!zip64UncompressedSize) {
9901
+ setUint32(headerView, 18, uncompressedSize);
9902
+ }
9903
+ if (!zip64CompressedSize) {
9904
+ setUint32(headerView, 14, compressedSize);
9905
+ }
9836
9906
  arraySet(directoryArray, headerArray, offset + 6);
9837
9907
  setUint16(directoryView, offset + 30, extraFieldLength);
9838
9908
  setUint16(directoryView, offset + 32, getLength(rawComment));
9839
- setUint16(directoryView, offset + 34, zip64 ? MAX_16_BITS : diskNumberStart);
9909
+ setUint16(directoryView, offset + 34, zip64 && zip64DiskNumberStart ? MAX_16_BITS : diskNumberStart);
9840
9910
  setUint16(directoryView, offset + 36, internalFileAttribute);
9841
9911
  if (externalFileAttribute) {
9842
9912
  setUint32(directoryView, offset + 38, externalFileAttribute);
9843
9913
  } else if (directory && msDosCompatible) {
9844
9914
  setUint8(directoryView, offset + 38, FILE_ATTR_MSDOS_DIR_MASK);
9845
9915
  }
9846
- setUint32(directoryView, offset + 42, zip64 ? MAX_32_BITS : fileEntryOffset);
9916
+ setUint32(directoryView, offset + 42, zip64 && zip64Offset ? MAX_32_BITS : fileEntryOffset);
9847
9917
  arraySet(directoryArray, rawFilename, offset + 46);
9848
9918
  arraySet(directoryArray, rawExtraFieldZip64, offset + 46 + getLength(rawFilename));
9849
9919
  arraySet(directoryArray, rawExtraFieldAES, offset + 46 + getLength(rawFilename, rawExtraFieldZip64));
@@ -9897,7 +9967,11 @@
9897
9967
  setUint32(endOfdirectoryView, 56, ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIGNATURE);
9898
9968
  setBigUint64(endOfdirectoryView, 64, BigInt(directoryOffset) + BigInt(directoryDataLength));
9899
9969
  setUint32(endOfdirectoryView, 72, lastDiskNumber + 1);
9900
- diskNumber = MAX_16_BITS;
9970
+ const supportZip64SplitFile = getOptionValue(zipWriter, options, "supportZip64SplitFile", true);
9971
+ if (supportZip64SplitFile) {
9972
+ lastDiskNumber = MAX_16_BITS;
9973
+ diskNumber = MAX_16_BITS;
9974
+ }
9901
9975
  filesLength = MAX_16_BITS;
9902
9976
  directoryOffset = MAX_32_BITS;
9903
9977
  directoryDataLength = MAX_32_BITS;