@zip.js/zip.js 2.18.0 → 2.18.2

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.
package/index.cjs CHANGED
@@ -6027,32 +6027,19 @@ async function detectOverlappingEntry({
6027
6027
  }) {
6028
6028
  let dataDescriptorLength = 0;
6029
6029
  if (dataDescriptor) {
6030
- if (extraFieldZip64) {
6031
- dataDescriptorLength = DATA_DESCRIPTOR_RECORD_ZIP_64_LENGTH;
6032
- } else {
6033
- dataDescriptorLength = DATA_DESCRIPTOR_RECORD_LENGTH;
6034
- }
6035
- }
6036
- if (dataDescriptorLength) {
6037
- const dataDescriptorArray = await readUint8Array(reader, dataOffset + compressedSize, dataDescriptorLength + DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH);
6030
+ const zip64 = Boolean(extraFieldZip64);
6031
+ const dataDescriptorArray = await readUint8Array(reader, dataOffset + compressedSize, DATA_DESCRIPTOR_RECORD_ZIP_64_LENGTH + DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH);
6038
6032
  const dataDescriptorView = getDataView(dataDescriptorArray);
6039
- let signature = dataDescriptorArray.length == dataDescriptorLength + DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH &&
6040
- getUint32$1(dataDescriptorView, 0) == DATA_DESCRIPTOR_RECORD_SIGNATURE;
6041
- if (signature) {
6042
- const signedDataDescriptor = readDataDescriptor(dataDescriptorView, DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH, extraFieldZip64);
6043
- const matchCrc32 = (fileEntry.encrypted && !fileEntry.zipCrypto) || signedDataDescriptor.crc32 == crc32;
6044
- if (matchCrc32 &&
6045
- signedDataDescriptor.compressedSize == compressedSize &&
6046
- signedDataDescriptor.uncompressedSize == uncompressedSize) {
6047
- dataDescriptorLength += DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH;
6048
- } else {
6049
- signature = false;
6050
- }
6051
- }
6052
- if (dataDescriptorArray.length >= dataDescriptorLength) {
6053
- const localDataDescriptor = readDataDescriptor(dataDescriptorView, signature ? DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH : 0, extraFieldZip64);
6054
- localDataDescriptor.signature = signature;
6033
+ const candidates = [[zip64, true], [zip64, false], [!zip64, true], [!zip64, false]]
6034
+ .map(([zip64Layout, signature]) => readDataDescriptor(dataDescriptorView, zip64Layout, signature))
6035
+ .filter(candidate => candidate && candidate.compressedSize == compressedSize && candidate.uncompressedSize == uncompressedSize);
6036
+ const localDataDescriptor = candidates.find(candidate => candidate.crc32 == crc32) || candidates[0] ||
6037
+ readDataDescriptor(dataDescriptorView, zip64, true) || readDataDescriptor(dataDescriptorView, zip64, false);
6038
+ if (localDataDescriptor) {
6055
6039
  fileEntry.localDirectory.dataDescriptor = localDataDescriptor;
6040
+ dataDescriptorLength = getDataDescriptorLength(localDataDescriptor.zip64, localDataDescriptor.signature);
6041
+ } else {
6042
+ dataDescriptorLength = getDataDescriptorLength(zip64, false);
6056
6043
  }
6057
6044
  }
6058
6045
  const range = {
@@ -6117,18 +6104,31 @@ function mergeRanges(sortedRanges, pendingRanges) {
6117
6104
  return mergedRanges;
6118
6105
  }
6119
6106
 
6120
- function readDataDescriptor(dataDescriptorView, offset, extraFieldZip64) {
6107
+ function readDataDescriptor(dataDescriptorView, zip64, signature) {
6108
+ const offset = signature ? DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH : 0;
6109
+ if (dataDescriptorView.byteLength < getDataDescriptorLength(zip64, signature) ||
6110
+ (signature && getUint32$1(dataDescriptorView, 0) != DATA_DESCRIPTOR_RECORD_SIGNATURE)) {
6111
+ return UNDEFINED_VALUE;
6112
+ }
6121
6113
  const crc32 = getUint32$1(dataDescriptorView, offset);
6122
6114
  let compressedSize;
6123
6115
  let uncompressedSize;
6124
- if (extraFieldZip64) {
6125
- compressedSize = getBigUint64(dataDescriptorView, offset + 4);
6126
- uncompressedSize = getBigUint64(dataDescriptorView, offset + 12);
6127
- } else {
6128
- compressedSize = getUint32$1(dataDescriptorView, offset + 4);
6129
- uncompressedSize = getUint32$1(dataDescriptorView, offset + 8);
6116
+ try {
6117
+ if (zip64) {
6118
+ compressedSize = getBigUint64(dataDescriptorView, offset + 4);
6119
+ uncompressedSize = getBigUint64(dataDescriptorView, offset + 12);
6120
+ } else {
6121
+ compressedSize = getUint32$1(dataDescriptorView, offset + 4);
6122
+ uncompressedSize = getUint32$1(dataDescriptorView, offset + 8);
6123
+ }
6124
+ } catch {
6125
+ return UNDEFINED_VALUE;
6130
6126
  }
6131
- return { crc32, compressedSize, uncompressedSize };
6127
+ return { signature, zip64, crc32, compressedSize, uncompressedSize };
6128
+ }
6129
+
6130
+ function getDataDescriptorLength(zip64, signature) {
6131
+ return (zip64 ? DATA_DESCRIPTOR_RECORD_ZIP_64_LENGTH : DATA_DESCRIPTOR_RECORD_LENGTH) + (signature ? DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH : 0);
6132
6132
  }
6133
6133
 
6134
6134
  function getDiskOffset$1(reader, diskNumber) {
@@ -9049,7 +9049,7 @@ function formatSupported(StreamClass, format) {
9049
9049
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9050
9050
  */
9051
9051
 
9052
- const VERSION = "2.18.0";
9052
+ const VERSION = "2.18.2";
9053
9053
 
9054
9054
  /*
9055
9055
  Copyright (c) 2025 Gildas Lormeau. All rights reserved.
package/index.d.cts CHANGED
@@ -2240,10 +2240,23 @@ export interface LocalDataDescriptor {
2240
2240
  * `true` if the record is preceded by its optional signature.
2241
2241
  *
2242
2242
  * The signature is not part of the original format, it is a later convention writers are free to follow. It is
2243
- * reported as absent when the values following it disagree with the central directory, since the record is then
2244
- * read as starting at the first byte.
2243
+ * `false` when the record is read as starting at its first byte, see {@link LocalDataDescriptor#zip64} for how the
2244
+ * layout is chosen.
2245
2245
  */
2246
2246
  signature: boolean;
2247
+ /**
2248
+ * `true` if the sizes are stored as 8-byte values.
2249
+ *
2250
+ * Neither record tells the width reliably: the local file header is written before a streaming writer knows the
2251
+ * sizes, and the Zip64 extra field of the central directory record, written last, describes that record, not the
2252
+ * descriptor, so e.g. an entry placed past 4 GB can carry a Zip64 extra field in its central directory record for
2253
+ * its offset alone and a descriptor with 4-byte sizes. The record is therefore read with the layout, among the two
2254
+ * widths with and without the signature, whose sizes agree with the central directory, its CRC-32 too when the
2255
+ * central directory stores one and several layouts qualify; when none does, the width announced by the Zip64 extra
2256
+ * field of the local file header or of the central directory record is used, with the signature when the record
2257
+ * starts with one.
2258
+ */
2259
+ zip64: boolean;
2247
2260
  /**
2248
2261
  * The CRC-32 checksum stored in the record, which is allowed to differ from {@link EntryMetaData#crc32}.
2249
2262
  */
package/index.d.ts CHANGED
@@ -2240,10 +2240,23 @@ export interface LocalDataDescriptor {
2240
2240
  * `true` if the record is preceded by its optional signature.
2241
2241
  *
2242
2242
  * The signature is not part of the original format, it is a later convention writers are free to follow. It is
2243
- * reported as absent when the values following it disagree with the central directory, since the record is then
2244
- * read as starting at the first byte.
2243
+ * `false` when the record is read as starting at its first byte, see {@link LocalDataDescriptor#zip64} for how the
2244
+ * layout is chosen.
2245
2245
  */
2246
2246
  signature: boolean;
2247
+ /**
2248
+ * `true` if the sizes are stored as 8-byte values.
2249
+ *
2250
+ * Neither record tells the width reliably: the local file header is written before a streaming writer knows the
2251
+ * sizes, and the Zip64 extra field of the central directory record, written last, describes that record, not the
2252
+ * descriptor, so e.g. an entry placed past 4 GB can carry a Zip64 extra field in its central directory record for
2253
+ * its offset alone and a descriptor with 4-byte sizes. The record is therefore read with the layout, among the two
2254
+ * widths with and without the signature, whose sizes agree with the central directory, its CRC-32 too when the
2255
+ * central directory stores one and several layouts qualify; when none does, the width announced by the Zip64 extra
2256
+ * field of the local file header or of the central directory record is used, with the signature when the record
2257
+ * starts with one.
2258
+ */
2259
+ zip64: boolean;
2247
2260
  /**
2248
2261
  * The CRC-32 checksum stored in the record, which is allowed to differ from {@link EntryMetaData#crc32}.
2249
2262
  */