@zip.js/zip.js 2.7.63 → 2.7.64

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/dist/zip-full.js CHANGED
@@ -4293,6 +4293,8 @@
4293
4293
 
4294
4294
  const DIRECTORY_SIGNATURE = "/";
4295
4295
 
4296
+ const HEADER_SIZE = 26;
4297
+
4296
4298
  const MAX_DATE = new Date(2107, 11, 31);
4297
4299
  const MIN_DATE = new Date(1980, 0, 1);
4298
4300
 
@@ -7242,6 +7244,7 @@
7242
7244
 
7243
7245
  constructor(array) {
7244
7246
  super();
7247
+ array = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
7245
7248
  Object.assign(this, {
7246
7249
  array,
7247
7250
  size: array.length
@@ -7936,7 +7939,8 @@
7936
7939
  compressedSize
7937
7940
  } = zipEntry;
7938
7941
  const localDirectory = fileEntry.localDirectory = {};
7939
- const dataArray = await readUint8Array(reader, offset, 30, diskNumberStart);
7942
+ const dataArraySize = HEADER_SIZE + 4;
7943
+ const dataArray = await readUint8Array(reader, offset, dataArraySize + 4, diskNumberStart);
7940
7944
  const dataView = getDataView$1(dataArray);
7941
7945
  let password = getOptionValue$1(zipEntry, options, "password");
7942
7946
  let rawPassword = getOptionValue$1(zipEntry, options, "rawPassword");
@@ -7956,7 +7960,7 @@
7956
7960
  }
7957
7961
  readCommonHeader(localDirectory, dataView, 4);
7958
7962
  localDirectory.rawExtraField = localDirectory.extraFieldLength ?
7959
- await readUint8Array(reader, offset + 30 + localDirectory.filenameLength, localDirectory.extraFieldLength, diskNumberStart) :
7963
+ await readUint8Array(reader, offset + dataArraySize + localDirectory.filenameLength, localDirectory.extraFieldLength, diskNumberStart) :
7960
7964
  new Uint8Array();
7961
7965
  readCommonFooter(zipEntry, localDirectory, dataView, 4, true);
7962
7966
  Object.assign(fileEntry, {
@@ -7975,7 +7979,7 @@
7975
7979
  throw new Error(ERR_ENCRYPTED);
7976
7980
  }
7977
7981
  }
7978
- const dataOffset = offset + 30 + localDirectory.filenameLength + localDirectory.extraFieldLength;
7982
+ const dataOffset = offset + dataArraySize + localDirectory.filenameLength + localDirectory.extraFieldLength;
7979
7983
  const size = compressedSize;
7980
7984
  const readable = reader.readable;
7981
7985
  Object.assign(readable, {
@@ -8337,6 +8341,7 @@
8337
8341
  const ERR_INVALID_EXTRAFIELD_DATA = "Extra field data exceeds 64KB";
8338
8342
  const ERR_UNSUPPORTED_FORMAT = "Zip64 is not supported (make sure 'keepOrder' is set to 'true')";
8339
8343
  const ERR_UNDEFINED_UNCOMPRESSED_SIZE = "Undefined uncompressed size";
8344
+ const ERR_ZIP_NOT_EMPTY = "Zip file not empty";
8340
8345
 
8341
8346
  const EXTRAFIELD_DATA_AES = new Uint8Array([0x07, 0x00, 0x02, 0x00, 0x41, 0x45, 0x03, 0x00, 0x00]);
8342
8347
 
@@ -8364,6 +8369,99 @@
8364
8369
  });
8365
8370
  }
8366
8371
 
8372
+ async prependZip(reader) {
8373
+ if (this.filenames.size) {
8374
+ throw new Error(ERR_ZIP_NOT_EMPTY);
8375
+ }
8376
+ ({ reader } = initReader(reader));
8377
+ const [zipEntriesReader, zipDataReader] = reader.readable.tee();
8378
+ const zipReader = new ZipReader(new ReadableStream({
8379
+ async start(controller) {
8380
+ const reader = zipEntriesReader.getReader();
8381
+ await readChunk();
8382
+
8383
+ async function readChunk() {
8384
+ const { done, value } = await reader.read();
8385
+ if (done) {
8386
+ controller.close();
8387
+ } else {
8388
+ controller.enqueue(value);
8389
+ await readChunk();
8390
+ }
8391
+ }
8392
+ }
8393
+ }));
8394
+ const entries = await zipReader.getEntries();
8395
+ await zipReader.close();
8396
+ await zipDataReader.pipeTo(this.writer.writable, { preventClose: true, preventAbort: true });
8397
+ this.writer.writable.size = this.offset = reader.size;
8398
+ this.filenames = new Set(entries.map(entry => entry.filename));
8399
+ this.files = new Map(entries.map(entry => {
8400
+ const {
8401
+ version,
8402
+ compressionMethod,
8403
+ lastModDate,
8404
+ lastAccessDate,
8405
+ creationDate,
8406
+ rawFilename,
8407
+ bitFlag,
8408
+ encrypted,
8409
+ uncompressedSize,
8410
+ compressedSize,
8411
+ diskOffset,
8412
+ diskNumber,
8413
+ zip64
8414
+ } = entry;
8415
+ let {
8416
+ rawExtraFieldZip64,
8417
+ rawExtraFieldAES,
8418
+ rawExtraFieldExtendedTimestamp,
8419
+ rawExtraFieldNTFS,
8420
+ rawExtraField,
8421
+ } = entry;
8422
+ const { level, languageEncodingFlag, dataDescriptor } = bitFlag;
8423
+ rawExtraFieldZip64 = rawExtraFieldZip64 || new Uint8Array();
8424
+ rawExtraFieldAES = rawExtraFieldAES || new Uint8Array();
8425
+ rawExtraFieldExtendedTimestamp = rawExtraFieldExtendedTimestamp || new Uint8Array();
8426
+ rawExtraFieldNTFS = rawExtraFieldNTFS || new Uint8Array();
8427
+ rawExtraField = rawExtraField || new Uint8Array();
8428
+ const extraFieldLength = getLength(rawExtraFieldZip64, rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS, rawExtraField);
8429
+ const zip64UncompressedSize = zip64 && uncompressedSize > MAX_32_BITS;
8430
+ const zip64CompressedSize = zip64 && compressedSize > MAX_32_BITS;
8431
+ const {
8432
+ headerArray,
8433
+ headerView
8434
+ } = getHeaderArrayData({
8435
+ version,
8436
+ bitflag: getBitflag(level, languageEncodingFlag, dataDescriptor, encrypted),
8437
+ compressionMethod,
8438
+ uncompressedSize,
8439
+ compressedSize,
8440
+ lastModDate,
8441
+ rawFilename,
8442
+ zip64CompressedSize,
8443
+ zip64UncompressedSize,
8444
+ extraFieldLength
8445
+ });
8446
+ Object.assign(entry, {
8447
+ zip64UncompressedSize,
8448
+ zip64CompressedSize,
8449
+ zip64Offset: zip64 && this.offset - diskOffset > MAX_32_BITS,
8450
+ zip64DiskNumberStart: zip64 && diskNumber > MAX_16_BITS,
8451
+ rawExtraFieldZip64,
8452
+ rawExtraFieldAES,
8453
+ rawExtraFieldExtendedTimestamp,
8454
+ rawExtraFieldNTFS,
8455
+ rawExtraField,
8456
+ extendedTimestamp: rawExtraFieldExtendedTimestamp.length > 0 || rawExtraFieldNTFS.length > 0,
8457
+ extraFieldExtendedTimestampFlag: 0x1 + (lastAccessDate ? 0x2 : 0) + (creationDate ? 0x4 : 0),
8458
+ headerArray,
8459
+ headerView
8460
+ });
8461
+ return [entry.filename, entry];
8462
+ }));
8463
+ }
8464
+
8367
8465
  async add(name = "", reader, options = {}) {
8368
8466
  const zipWriter = this;
8369
8467
  const {
@@ -8399,6 +8497,22 @@
8399
8497
  }
8400
8498
  }
8401
8499
 
8500
+ remove(entry) {
8501
+ const { filenames, files } = this;
8502
+ if (typeof entry == "string") {
8503
+ entry = files.get(entry);
8504
+ }
8505
+ if (entry && entry.filename !== UNDEFINED_VALUE) {
8506
+ const { filename } = entry;
8507
+ if (filenames.has(filename) && files.has(filename)) {
8508
+ filenames.delete(filename);
8509
+ files.delete(filename);
8510
+ return true;
8511
+ }
8512
+ }
8513
+ return false;
8514
+ }
8515
+
8402
8516
  async close(comment = new Uint8Array(), options = {}) {
8403
8517
  const zipWriter = this;
8404
8518
  const { pendingAddFileCalls, writer } = this;
@@ -8520,7 +8634,13 @@
8520
8634
  const useUnicodeFileNames = getOptionValue(zipWriter, options, "useUnicodeFileNames", true);
8521
8635
  const useCompressionStream = getOptionValue(zipWriter, options, "useCompressionStream");
8522
8636
  const compressionMethod = getOptionValue(zipWriter, options, "compressionMethod");
8523
- let dataDescriptor = getOptionValue(zipWriter, options, "dataDescriptor", true);
8637
+ let dataDescriptor = getOptionValue(zipWriter, options, "dataDescriptor");
8638
+ if (bufferedWrite && dataDescriptor === UNDEFINED_VALUE) {
8639
+ dataDescriptor = false;
8640
+ }
8641
+ if (dataDescriptor === UNDEFINED_VALUE || zipCrypto) {
8642
+ dataDescriptor = true;
8643
+ }
8524
8644
  let zip64 = getOptionValue(zipWriter, options, PROPERTY_NAME_ZIP64);
8525
8645
  if (!zipCrypto && (password !== UNDEFINED_VALUE || rawPassword !== UNDEFINED_VALUE) && !(encryptionStrength >= 1 && encryptionStrength <= 3)) {
8526
8646
  throw new Error(ERR_INVALID_ENCRYPTION_STRENGTH);
@@ -8566,10 +8686,11 @@
8566
8686
  uncompressedSize = maximumCompressedSize = MAX_32_BITS + 1;
8567
8687
  }
8568
8688
  } else {
8569
- uncompressedSize = reader.size;
8689
+ options.uncompressedSize = uncompressedSize = reader.size;
8570
8690
  maximumCompressedSize = getMaximumCompressedSize(uncompressedSize);
8571
8691
  }
8572
8692
  } else {
8693
+ options.uncompressedSize = uncompressedSize;
8573
8694
  maximumCompressedSize = getMaximumCompressedSize(uncompressedSize);
8574
8695
  }
8575
8696
  }
@@ -8625,7 +8746,10 @@
8625
8746
  passThrough,
8626
8747
  encrypted: Boolean((password && getLength(password)) || (rawPassword && getLength(rawPassword))) || (passThrough && encrypted),
8627
8748
  signature,
8628
- compressionMethod
8749
+ compressionMethod,
8750
+ uncompressedSize,
8751
+ offset: zipWriter.offset - diskOffset,
8752
+ diskNumberStart: diskNumber,
8629
8753
  });
8630
8754
  const headerInfo = getHeaderInfo(options);
8631
8755
  const dataDescriptorInfo = getDataDescriptorInfo(options);
@@ -8717,20 +8841,26 @@
8717
8841
  await lockPreviousFileEntry;
8718
8842
  await requestLockWriter();
8719
8843
  writingBufferedEntryData = true;
8720
- if (!dataDescriptor) {
8721
- blob = await writeExtraHeaderInfo(fileEntry, blob, writable, options);
8722
- }
8723
- await skipDiskIfNeeded(writable);
8724
8844
  fileEntry.diskNumberStart = writer.diskNumber;
8725
8845
  diskOffset = writer.diskOffset;
8846
+ fileEntry.offset = zipWriter.offset - diskOffset;
8847
+ if (fileEntry.zip64) {
8848
+ setExtraFieldZip64Data(fileEntry);
8849
+ }
8850
+ if (fileEntry.zip64 || !dataDescriptor) {
8851
+ blob = await patchLocalHeader(fileEntry, blob, writable, options);
8852
+ }
8853
+ await skipDiskIfNeeded(writable);
8726
8854
  await blob.stream().pipeTo(writable, { preventClose: true, preventAbort: true, signal });
8727
8855
  writable.size += blob.size;
8728
8856
  writingBufferedEntryData = false;
8857
+ } else {
8858
+ fileEntry.offset = zipWriter.offset - diskOffset;
8859
+ if (fileEntry.zip64) {
8860
+ setExtraFieldZip64Data(fileEntry);
8861
+ }
8729
8862
  }
8730
- fileEntry.offset = zipWriter.offset - diskOffset;
8731
- if (fileEntry.zip64) {
8732
- setZip64ExtraInfo(fileEntry, options);
8733
- } else if (fileEntry.offset > MAX_32_BITS) {
8863
+ if (fileEntry.offset > MAX_32_BITS && !fileEntry.zip64) {
8734
8864
  throw new Error(ERR_UNSUPPORTED_FORMAT);
8735
8865
  }
8736
8866
  zipWriter.offset += fileEntry.size;
@@ -8797,12 +8927,15 @@
8797
8927
  const {
8798
8928
  localHeaderArray,
8799
8929
  headerArray,
8930
+ headerView,
8800
8931
  lastModDate,
8801
8932
  rawLastModDate,
8802
8933
  encrypted,
8803
8934
  compressed,
8804
8935
  version,
8805
8936
  compressionMethod,
8937
+ rawExtraFieldZip64,
8938
+ localExtraFieldZip64Length,
8806
8939
  rawExtraFieldExtendedTimestamp,
8807
8940
  extraFieldExtendedTimestampFlag,
8808
8941
  rawExtraFieldNTFS,
@@ -8851,6 +8984,8 @@
8851
8984
  rawFilename,
8852
8985
  commentUTF8: true,
8853
8986
  rawComment,
8987
+ rawExtraFieldZip64,
8988
+ localExtraFieldZip64Length,
8854
8989
  rawExtraFieldExtendedTimestamp,
8855
8990
  rawExtraFieldNTFS,
8856
8991
  rawExtraFieldAES,
@@ -8904,28 +9039,8 @@
8904
9039
  } else {
8905
9040
  await writeData(writable, localHeaderArray);
8906
9041
  }
8907
- let rawExtraFieldZip64;
8908
- if (zip64) {
8909
- let rawExtraFieldZip64Length = 4;
8910
- if (zip64UncompressedSize) {
8911
- rawExtraFieldZip64Length += 8;
8912
- }
8913
- if (zip64CompressedSize) {
8914
- rawExtraFieldZip64Length += 8;
8915
- }
8916
- if (zip64Offset) {
8917
- rawExtraFieldZip64Length += 8;
8918
- }
8919
- if (zip64DiskNumberStart) {
8920
- rawExtraFieldZip64Length += 4;
8921
- }
8922
- rawExtraFieldZip64 = new Uint8Array(rawExtraFieldZip64Length);
8923
- } else {
8924
- rawExtraFieldZip64 = new Uint8Array();
8925
- }
8926
9042
  setEntryInfo({
8927
9043
  signature,
8928
- rawExtraFieldZip64,
8929
9044
  compressedSize,
8930
9045
  uncompressedSize,
8931
9046
  headerInfo,
@@ -8947,8 +9062,8 @@
8947
9062
  compressionMethod,
8948
9063
  version,
8949
9064
  headerArray,
9065
+ headerView,
8950
9066
  signature,
8951
- rawExtraFieldZip64,
8952
9067
  extraFieldExtendedTimestampFlag,
8953
9068
  zip64UncompressedSize,
8954
9069
  zip64CompressedSize,
@@ -8973,10 +9088,62 @@
8973
9088
  rawExtraField,
8974
9089
  encryptionStrength,
8975
9090
  extendedTimestamp,
8976
- encrypted
9091
+ passThrough,
9092
+ encrypted,
9093
+ zip64UncompressedSize,
9094
+ zip64CompressedSize,
9095
+ zip64Offset,
9096
+ zip64DiskNumberStart,
9097
+ uncompressedSize,
9098
+ offset,
9099
+ diskNumberStart
8977
9100
  } = options;
8978
9101
  let { version, compressionMethod } = options;
8979
9102
  const compressed = !directory && (level > 0 || (level === UNDEFINED_VALUE && compressionMethod !== 0));
9103
+ let rawExtraFieldZip64;
9104
+ const uncompressedFile = passThrough || !compressed;
9105
+ const zip64ExtraFieldComplete = zip64 && (options.bufferedWrite || ((!zip64UncompressedSize && !zip64CompressedSize) || uncompressedFile));
9106
+ if (zip64) {
9107
+ let rawExtraFieldZip64Length = 4;
9108
+ if (zip64UncompressedSize) {
9109
+ rawExtraFieldZip64Length += 8;
9110
+ }
9111
+ if (zip64CompressedSize) {
9112
+ rawExtraFieldZip64Length += 8;
9113
+ }
9114
+ if (zip64Offset) {
9115
+ rawExtraFieldZip64Length += 8;
9116
+ }
9117
+ if (zip64DiskNumberStart) {
9118
+ rawExtraFieldZip64Length += 4;
9119
+ }
9120
+ rawExtraFieldZip64 = new Uint8Array(rawExtraFieldZip64Length);
9121
+ const rawExtraFieldZip64View = getDataView(rawExtraFieldZip64);
9122
+ setUint16(rawExtraFieldZip64View, 0, EXTRAFIELD_TYPE_ZIP64);
9123
+ setUint16(rawExtraFieldZip64View, 2, getLength(rawExtraFieldZip64) - 4);
9124
+ if (zip64ExtraFieldComplete) {
9125
+ const rawExtraFieldZip64View = getDataView(rawExtraFieldZip64);
9126
+ let rawExtraFieldZip64Offset = 4;
9127
+ if (zip64UncompressedSize) {
9128
+ setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(uncompressedSize));
9129
+ rawExtraFieldZip64Offset += 8;
9130
+ }
9131
+ if (zip64CompressedSize && uncompressedFile) {
9132
+ setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(uncompressedSize));
9133
+ rawExtraFieldZip64Offset += 8;
9134
+ }
9135
+ if (zip64Offset) {
9136
+ setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(offset));
9137
+ rawExtraFieldZip64Offset += 8;
9138
+ }
9139
+ if (zip64DiskNumberStart) {
9140
+ setUint32(rawExtraFieldZip64View, rawExtraFieldZip64Offset, diskNumberStart);
9141
+ rawExtraFieldZip64Offset += 4;
9142
+ }
9143
+ }
9144
+ } else {
9145
+ rawExtraFieldZip64 = new Uint8Array();
9146
+ }
8980
9147
  let rawExtraFieldAES;
8981
9148
  if (encrypted && !zipCrypto) {
8982
9149
  rawExtraFieldAES = new Uint8Array(getLength(EXTRAFIELD_DATA_AES) + 2);
@@ -9025,69 +9192,52 @@
9025
9192
  } else {
9026
9193
  rawExtraFieldNTFS = rawExtraFieldExtendedTimestamp = new Uint8Array();
9027
9194
  }
9028
- let bitFlag = 0;
9029
- if (useUnicodeFileNames) {
9030
- bitFlag = bitFlag | BITFLAG_LANG_ENCODING_FLAG;
9031
- }
9032
- if (dataDescriptor) {
9033
- bitFlag = bitFlag | BITFLAG_DATA_DESCRIPTOR;
9034
- }
9035
9195
  if (compressionMethod === UNDEFINED_VALUE) {
9036
9196
  compressionMethod = compressed ? COMPRESSION_METHOD_DEFLATE : COMPRESSION_METHOD_STORE;
9037
9197
  }
9038
- if (compressionMethod == COMPRESSION_METHOD_DEFLATE) {
9039
- if (level >= 1 && level < 3) {
9040
- bitFlag = bitFlag | 0b110;
9041
- }
9042
- if (level >= 3 && level < 5) {
9043
- bitFlag = bitFlag | 0b01;
9044
- }
9045
- if (level === 9) {
9046
- bitFlag = bitFlag | 0b10;
9047
- }
9048
- }
9049
9198
  if (zip64) {
9050
9199
  version = version > VERSION_ZIP64 ? version : VERSION_ZIP64;
9051
9200
  }
9052
- if (encrypted) {
9053
- bitFlag = bitFlag | BITFLAG_ENCRYPTED;
9054
- if (!zipCrypto) {
9055
- version = version > VERSION_AES ? version : VERSION_AES;
9056
- rawExtraFieldAES[9] = compressionMethod;
9057
- compressionMethod = COMPRESSION_METHOD_AES;
9058
- }
9059
- }
9060
- const headerArray = new Uint8Array(26);
9061
- const headerView = getDataView(headerArray);
9062
- setUint16(headerView, 0, version);
9063
- setUint16(headerView, 2, bitFlag);
9064
- setUint16(headerView, 4, compressionMethod);
9065
- const dateArray = new Uint32Array(1);
9066
- const dateView = getDataView(dateArray);
9067
- let lastModDateMsDos;
9068
- if (lastModDate < MIN_DATE) {
9069
- lastModDateMsDos = MIN_DATE;
9070
- } else if (lastModDate > MAX_DATE) {
9071
- lastModDateMsDos = MAX_DATE;
9072
- } else {
9073
- lastModDateMsDos = lastModDate;
9201
+ if (encrypted && !zipCrypto) {
9202
+ version = version > VERSION_AES ? version : VERSION_AES;
9203
+ rawExtraFieldAES[9] = compressionMethod;
9204
+ compressionMethod = COMPRESSION_METHOD_AES;
9074
9205
  }
9075
- setUint16(dateView, 0, (((lastModDateMsDos.getHours() << 6) | lastModDateMsDos.getMinutes()) << 5) | lastModDateMsDos.getSeconds() / 2);
9076
- setUint16(dateView, 2, ((((lastModDateMsDos.getFullYear() - 1980) << 4) | (lastModDateMsDos.getMonth() + 1)) << 5) | lastModDateMsDos.getDate());
9077
- const rawLastModDate = dateArray[0];
9078
- setUint32(headerView, 6, rawLastModDate);
9079
- setUint16(headerView, 22, getLength(rawFilename));
9080
- const extraFieldLength = getLength(rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS, rawExtraField);
9081
- setUint16(headerView, 24, extraFieldLength);
9082
- const localHeaderArray = new Uint8Array(30 + getLength(rawFilename) + extraFieldLength);
9206
+ const localExtraFieldZip64Length = zip64ExtraFieldComplete ? getLength(rawExtraFieldZip64) : 0;
9207
+ const extraFieldLength = localExtraFieldZip64Length + getLength(rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS, rawExtraField);
9208
+ const {
9209
+ headerArray,
9210
+ headerView,
9211
+ rawLastModDate
9212
+ } = getHeaderArrayData({
9213
+ version,
9214
+ bitflag: getBitflag(level, useUnicodeFileNames, dataDescriptor, encrypted),
9215
+ compressionMethod,
9216
+ uncompressedSize,
9217
+ lastModDate: lastModDate < MIN_DATE ? MIN_DATE : lastModDate > MAX_DATE ? MAX_DATE : lastModDate,
9218
+ rawFilename,
9219
+ zip64CompressedSize,
9220
+ zip64UncompressedSize,
9221
+ extraFieldLength
9222
+ });
9223
+ let localHeaderOffset = HEADER_SIZE + 4;
9224
+ const localHeaderArray = new Uint8Array(localHeaderOffset + getLength(rawFilename) + extraFieldLength);
9083
9225
  const localHeaderView = getDataView(localHeaderArray);
9084
9226
  setUint32(localHeaderView, 0, LOCAL_FILE_HEADER_SIGNATURE);
9085
9227
  arraySet(localHeaderArray, headerArray, 4);
9086
- arraySet(localHeaderArray, rawFilename, 30);
9087
- arraySet(localHeaderArray, rawExtraFieldAES, 30 + getLength(rawFilename));
9088
- arraySet(localHeaderArray, rawExtraFieldExtendedTimestamp, 30 + getLength(rawFilename, rawExtraFieldAES));
9089
- arraySet(localHeaderArray, rawExtraFieldNTFS, 30 + getLength(rawFilename, rawExtraFieldAES, rawExtraFieldExtendedTimestamp));
9090
- arraySet(localHeaderArray, rawExtraField, 30 + getLength(rawFilename, rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS));
9228
+ arraySet(localHeaderArray, rawFilename, localHeaderOffset);
9229
+ localHeaderOffset += getLength(rawFilename);
9230
+ if (zip64ExtraFieldComplete) {
9231
+ arraySet(localHeaderArray, rawExtraFieldZip64, localHeaderOffset);
9232
+ }
9233
+ localHeaderOffset += localExtraFieldZip64Length;
9234
+ arraySet(localHeaderArray, rawExtraFieldAES, localHeaderOffset);
9235
+ localHeaderOffset += getLength(rawExtraFieldAES);
9236
+ arraySet(localHeaderArray, rawExtraFieldExtendedTimestamp, localHeaderOffset);
9237
+ localHeaderOffset += getLength(rawExtraFieldExtendedTimestamp);
9238
+ arraySet(localHeaderArray, rawExtraFieldNTFS, localHeaderOffset);
9239
+ localHeaderOffset += getLength(rawExtraFieldNTFS);
9240
+ arraySet(localHeaderArray, rawExtraField, localHeaderOffset);
9091
9241
  return {
9092
9242
  localHeaderArray,
9093
9243
  headerArray,
@@ -9099,6 +9249,8 @@
9099
9249
  version,
9100
9250
  compressionMethod,
9101
9251
  extraFieldExtendedTimestampFlag,
9252
+ rawExtraFieldZip64,
9253
+ localExtraFieldZip64Length,
9102
9254
  rawExtraFieldExtendedTimestamp,
9103
9255
  rawExtraFieldNTFS,
9104
9256
  rawExtraFieldAES,
@@ -9153,7 +9305,6 @@
9153
9305
  function setEntryInfo(entryInfo, options) {
9154
9306
  const {
9155
9307
  signature,
9156
- rawExtraFieldZip64,
9157
9308
  compressedSize,
9158
9309
  uncompressedSize,
9159
9310
  headerInfo,
@@ -9169,8 +9320,6 @@
9169
9320
  } = dataDescriptorInfo;
9170
9321
  const {
9171
9322
  zip64,
9172
- zip64UncompressedSize,
9173
- zip64CompressedSize,
9174
9323
  zipCrypto,
9175
9324
  dataDescriptor
9176
9325
  } = options;
@@ -9181,19 +9330,6 @@
9181
9330
  }
9182
9331
  }
9183
9332
  if (zip64) {
9184
- const rawExtraFieldZip64View = getDataView(rawExtraFieldZip64);
9185
- setUint16(rawExtraFieldZip64View, 0, EXTRAFIELD_TYPE_ZIP64);
9186
- setUint16(rawExtraFieldZip64View, 2, getLength(rawExtraFieldZip64) - 4);
9187
- let rawExtraFieldZip64Offset = 4;
9188
- if (zip64UncompressedSize) {
9189
- setUint32(headerView, 18, MAX_32_BITS);
9190
- setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(uncompressedSize));
9191
- rawExtraFieldZip64Offset += 8;
9192
- }
9193
- if (zip64CompressedSize) {
9194
- setUint32(headerView, 14, MAX_32_BITS);
9195
- setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(compressedSize));
9196
- }
9197
9333
  if (dataDescriptor) {
9198
9334
  setBigUint64(dataDescriptorView, dataDescriptorOffset + 4, BigInt(compressedSize));
9199
9335
  setBigUint64(dataDescriptorView, dataDescriptorOffset + 12, BigInt(uncompressedSize));
@@ -9208,44 +9344,82 @@
9208
9344
  }
9209
9345
  }
9210
9346
 
9211
- async function writeExtraHeaderInfo(fileEntry, entryData, writable, { zipCrypto }) {
9347
+ async function patchLocalHeader(fileEntry, entryData, writable, { zipCrypto, dataDescriptor }) {
9348
+ const {
9349
+ zip64,
9350
+ rawFilename,
9351
+ localExtraFieldZip64Length,
9352
+ signature,
9353
+ encrypted,
9354
+ zip64UncompressedSize,
9355
+ zip64CompressedSize,
9356
+ zip64Offset,
9357
+ zip64DiskNumberStart,
9358
+ uncompressedSize,
9359
+ compressedSize,
9360
+ offset,
9361
+ diskNumberStart
9362
+ } = fileEntry;
9212
9363
  let arrayBuffer;
9213
- arrayBuffer = await entryData.slice(0, 26).arrayBuffer();
9214
- if (arrayBuffer.byteLength != 26) {
9215
- arrayBuffer = arrayBuffer.slice(0, 26);
9364
+ let rawExtraFieldZip64Offset = HEADER_SIZE + 4 + getLength(rawFilename);
9365
+ const arrayBufferSize = zip64 && localExtraFieldZip64Length ? rawExtraFieldZip64Offset + localExtraFieldZip64Length : HEADER_SIZE;
9366
+ arrayBuffer = await entryData.slice(0, arrayBufferSize).arrayBuffer();
9367
+ if (arrayBuffer.byteLength != arrayBufferSize) {
9368
+ arrayBuffer = arrayBuffer.slice(0, arrayBufferSize);
9216
9369
  }
9217
9370
  const arrayBufferView = new DataView(arrayBuffer);
9218
- if (!fileEntry.encrypted || zipCrypto) {
9219
- setUint32(arrayBufferView, 14, fileEntry.signature);
9371
+ if (!dataDescriptor) {
9372
+ if (!encrypted || zipCrypto) {
9373
+ setUint32(arrayBufferView, 14, signature);
9374
+ }
9375
+ if (!zip64) {
9376
+ setUint32(arrayBufferView, 18, compressedSize);
9377
+ setUint32(arrayBufferView, 22, uncompressedSize);
9378
+ }
9220
9379
  }
9221
- if (fileEntry.zip64) {
9380
+ if (zip64) {
9222
9381
  setUint32(arrayBufferView, 18, MAX_32_BITS);
9223
9382
  setUint32(arrayBufferView, 22, MAX_32_BITS);
9224
- } else {
9225
- setUint32(arrayBufferView, 18, fileEntry.compressedSize);
9226
- setUint32(arrayBufferView, 22, fileEntry.uncompressedSize);
9383
+ if (localExtraFieldZip64Length) {
9384
+ rawExtraFieldZip64Offset += 4;
9385
+ if (zip64UncompressedSize) {
9386
+ setBigUint64(arrayBufferView, rawExtraFieldZip64Offset, BigInt(uncompressedSize));
9387
+ rawExtraFieldZip64Offset += 8;
9388
+ }
9389
+ if (zip64CompressedSize) {
9390
+ setBigUint64(arrayBufferView, rawExtraFieldZip64Offset, BigInt(compressedSize));
9391
+ rawExtraFieldZip64Offset += 8;
9392
+ }
9393
+ if (zip64Offset) {
9394
+ setBigUint64(arrayBufferView, rawExtraFieldZip64Offset, BigInt(offset));
9395
+ rawExtraFieldZip64Offset += 8;
9396
+ }
9397
+ if (zip64DiskNumberStart) {
9398
+ setUint32(arrayBufferView, rawExtraFieldZip64Offset, diskNumberStart);
9399
+ }
9400
+ }
9227
9401
  }
9228
9402
  await writeData(writable, new Uint8Array(arrayBuffer));
9229
9403
  return entryData.slice(arrayBuffer.byteLength);
9230
9404
  }
9231
9405
 
9232
- function setZip64ExtraInfo(fileEntry, options) {
9233
- const { rawExtraFieldZip64, offset, diskNumberStart } = fileEntry;
9234
- const { zip64UncompressedSize, zip64CompressedSize, zip64Offset, zip64DiskNumberStart } = options;
9235
- const rawExtraFieldZip64View = getDataView(rawExtraFieldZip64);
9406
+ function setExtraFieldZip64Data(fileEntry) {
9407
+ const rawExtraFieldZip64View = getDataView(fileEntry.rawExtraFieldZip64);
9236
9408
  let rawExtraFieldZip64Offset = 4;
9237
- if (zip64UncompressedSize) {
9409
+ if (fileEntry.zip64UncompressedSize) {
9410
+ setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(fileEntry.uncompressedSize));
9238
9411
  rawExtraFieldZip64Offset += 8;
9239
9412
  }
9240
- if (zip64CompressedSize) {
9413
+ if (fileEntry.zip64CompressedSize) {
9414
+ setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(fileEntry.compressedSize));
9241
9415
  rawExtraFieldZip64Offset += 8;
9242
9416
  }
9243
- if (zip64Offset) {
9244
- setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(offset));
9417
+ if (fileEntry.zip64Offset) {
9418
+ setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(fileEntry.offset));
9245
9419
  rawExtraFieldZip64Offset += 8;
9246
9420
  }
9247
- if (zip64DiskNumberStart) {
9248
- setUint32(rawExtraFieldZip64View, rawExtraFieldZip64Offset, diskNumberStart);
9421
+ if (fileEntry.zip64DiskNumberStart) {
9422
+ setUint32(rawExtraFieldZip64View, rawExtraFieldZip64Offset, fileEntry.diskNumberStart);
9249
9423
  }
9250
9424
  }
9251
9425
 
@@ -9280,7 +9454,7 @@
9280
9454
  } else {
9281
9455
  rawExtraFieldTimestamp = new Uint8Array();
9282
9456
  }
9283
- fileEntry.rawExtraFieldCDExtendedTimestamp = rawExtraFieldTimestamp;
9457
+ fileEntry.rawExtraFieldExtendedTimestamp = rawExtraFieldTimestamp;
9284
9458
  directoryDataLength += 46 +
9285
9459
  getLength(
9286
9460
  rawFilename,
@@ -9301,12 +9475,13 @@
9301
9475
  rawFilename,
9302
9476
  rawExtraFieldZip64,
9303
9477
  rawExtraFieldAES,
9304
- rawExtraFieldCDExtendedTimestamp,
9478
+ rawExtraFieldExtendedTimestamp,
9305
9479
  rawExtraFieldNTFS,
9306
9480
  rawExtraField,
9307
9481
  rawComment,
9308
9482
  versionMadeBy,
9309
9483
  headerArray,
9484
+ headerView,
9310
9485
  zip64,
9311
9486
  zip64UncompressedSize,
9312
9487
  zip64CompressedSize,
@@ -9318,10 +9493,9 @@
9318
9493
  uncompressedSize,
9319
9494
  compressedSize
9320
9495
  } = fileEntry;
9321
- const extraFieldLength = getLength(rawExtraFieldZip64, rawExtraFieldAES, rawExtraFieldCDExtendedTimestamp, rawExtraFieldNTFS, rawExtraField);
9496
+ const extraFieldLength = getLength(rawExtraFieldZip64, rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS, rawExtraField);
9322
9497
  setUint32(directoryView, offset, CENTRAL_FILE_HEADER_SIGNATURE);
9323
9498
  setUint16(directoryView, offset + 4, versionMadeBy);
9324
- const headerView = getDataView(headerArray);
9325
9499
  if (!zip64UncompressedSize) {
9326
9500
  setUint32(headerView, 18, uncompressedSize);
9327
9501
  }
@@ -9329,28 +9503,40 @@
9329
9503
  setUint32(headerView, 14, compressedSize);
9330
9504
  }
9331
9505
  arraySet(directoryArray, headerArray, offset + 6);
9332
- setUint16(directoryView, offset + 30, extraFieldLength);
9333
- setUint16(directoryView, offset + 32, getLength(rawComment));
9334
- setUint16(directoryView, offset + 34, zip64 && zip64DiskNumberStart ? MAX_16_BITS : diskNumberStart);
9335
- setUint16(directoryView, offset + 36, internalFileAttributes);
9506
+ let directoryOffset = offset + HEADER_SIZE + 4;
9507
+ setUint16(directoryView, directoryOffset, extraFieldLength);
9508
+ directoryOffset += 2;
9509
+ setUint16(directoryView, directoryOffset, getLength(rawComment));
9510
+ directoryOffset += 2;
9511
+ setUint16(directoryView, directoryOffset, zip64 && zip64DiskNumberStart ? MAX_16_BITS : diskNumberStart);
9512
+ directoryOffset += 2;
9513
+ setUint16(directoryView, directoryOffset, internalFileAttributes);
9514
+ directoryOffset += 2;
9336
9515
  if (externalFileAttributes) {
9337
- setUint32(directoryView, offset + 38, externalFileAttributes);
9338
- }
9339
- setUint32(directoryView, offset + 42, zip64 && zip64Offset ? MAX_32_BITS : fileEntryOffset);
9340
- arraySet(directoryArray, rawFilename, offset + 46);
9341
- arraySet(directoryArray, rawExtraFieldZip64, offset + 46 + getLength(rawFilename));
9342
- arraySet(directoryArray, rawExtraFieldAES, offset + 46 + getLength(rawFilename, rawExtraFieldZip64));
9343
- arraySet(directoryArray, rawExtraFieldCDExtendedTimestamp, offset + 46 + getLength(rawFilename, rawExtraFieldZip64, rawExtraFieldAES));
9344
- arraySet(directoryArray, rawExtraFieldNTFS, offset + 46 + getLength(rawFilename, rawExtraFieldZip64, rawExtraFieldAES, rawExtraFieldCDExtendedTimestamp));
9345
- arraySet(directoryArray, rawExtraField, offset + 46 + getLength(rawFilename, rawExtraFieldZip64, rawExtraFieldAES, rawExtraFieldCDExtendedTimestamp, rawExtraFieldNTFS));
9346
- arraySet(directoryArray, rawComment, offset + 46 + getLength(rawFilename) + extraFieldLength);
9347
- const directoryEntryLength = 46 + getLength(rawFilename, rawComment) + extraFieldLength;
9516
+ setUint32(directoryView, directoryOffset, externalFileAttributes);
9517
+ }
9518
+ directoryOffset += 4;
9519
+ setUint32(directoryView, directoryOffset, zip64 && zip64Offset ? MAX_32_BITS : fileEntryOffset);
9520
+ directoryOffset += 4;
9521
+ arraySet(directoryArray, rawFilename, directoryOffset);
9522
+ directoryOffset += getLength(rawFilename);
9523
+ arraySet(directoryArray, rawExtraFieldZip64, directoryOffset);
9524
+ directoryOffset += getLength(rawExtraFieldZip64);
9525
+ arraySet(directoryArray, rawExtraFieldAES, directoryOffset);
9526
+ directoryOffset += getLength(rawExtraFieldAES);
9527
+ arraySet(directoryArray, rawExtraFieldExtendedTimestamp, directoryOffset);
9528
+ directoryOffset += getLength(rawExtraFieldExtendedTimestamp);
9529
+ arraySet(directoryArray, rawExtraFieldNTFS, directoryOffset);
9530
+ directoryOffset += getLength(rawExtraFieldNTFS);
9531
+ arraySet(directoryArray, rawExtraField, directoryOffset);
9532
+ directoryOffset += getLength(rawExtraField);
9533
+ arraySet(directoryArray, rawComment, directoryOffset);
9348
9534
  if (offset - directoryDiskOffset > writer.availableSize) {
9349
9535
  writer.availableSize = 0;
9350
9536
  await writeData(writable, directoryArray.slice(directoryDiskOffset, offset));
9351
9537
  directoryDiskOffset = offset;
9352
9538
  }
9353
- offset += directoryEntryLength;
9539
+ offset = directoryOffset;
9354
9540
  if (options.onprogress) {
9355
9541
  try {
9356
9542
  await options.onprogress(indexFileEntry + 1, files.size, new Entry(fileEntry));
@@ -9478,6 +9664,67 @@
9478
9664
  return result;
9479
9665
  }
9480
9666
 
9667
+ function getHeaderArrayData({
9668
+ version,
9669
+ bitflag,
9670
+ compressionMethod,
9671
+ uncompressedSize,
9672
+ compressedSize,
9673
+ lastModDate,
9674
+ rawFilename,
9675
+ zip64CompressedSize,
9676
+ zip64UncompressedSize,
9677
+ extraFieldLength
9678
+ }) {
9679
+ const headerArray = new Uint8Array(HEADER_SIZE);
9680
+ const headerView = getDataView(headerArray);
9681
+ setUint16(headerView, 0, version);
9682
+ setUint16(headerView, 2, bitflag);
9683
+ setUint16(headerView, 4, compressionMethod);
9684
+ const dateArray = new Uint32Array(1);
9685
+ const dateView = getDataView(dateArray);
9686
+ setUint16(dateView, 0, (((lastModDate.getHours() << 6) | lastModDate.getMinutes()) << 5) | lastModDate.getSeconds() / 2);
9687
+ setUint16(dateView, 2, ((((lastModDate.getFullYear() - 1980) << 4) | (lastModDate.getMonth() + 1)) << 5) | lastModDate.getDate());
9688
+ const rawLastModDate = dateArray[0];
9689
+ setUint32(headerView, 6, rawLastModDate);
9690
+ if (zip64CompressedSize || compressedSize !== UNDEFINED_VALUE) {
9691
+ setUint32(headerView, 14, zip64CompressedSize ? MAX_32_BITS : compressedSize);
9692
+ }
9693
+ if (zip64UncompressedSize || uncompressedSize !== UNDEFINED_VALUE) {
9694
+ setUint32(headerView, 18, zip64UncompressedSize ? MAX_32_BITS : uncompressedSize);
9695
+ }
9696
+ setUint16(headerView, 22, getLength(rawFilename));
9697
+ setUint16(headerView, 24, extraFieldLength);
9698
+ return {
9699
+ headerArray,
9700
+ headerView,
9701
+ rawLastModDate
9702
+ };
9703
+ }
9704
+
9705
+ function getBitflag(level, useUnicodeFileNames, dataDescriptor, encrypted) {
9706
+ let bitFlag = 0;
9707
+ if (useUnicodeFileNames) {
9708
+ bitFlag = bitFlag | BITFLAG_LANG_ENCODING_FLAG;
9709
+ }
9710
+ if (dataDescriptor) {
9711
+ bitFlag = bitFlag | BITFLAG_DATA_DESCRIPTOR;
9712
+ }
9713
+ if (level >= 1 && level < 3) {
9714
+ bitFlag = bitFlag | 0b110;
9715
+ }
9716
+ if (level >= 3 && level < 5) {
9717
+ bitFlag = bitFlag | 0b01;
9718
+ }
9719
+ if (level === 9) {
9720
+ bitFlag = bitFlag | 0b10;
9721
+ }
9722
+ if (encrypted) {
9723
+ bitFlag = bitFlag | BITFLAG_ENCRYPTED;
9724
+ }
9725
+ return bitFlag;
9726
+ }
9727
+
9481
9728
  /*
9482
9729
  Copyright (c) 2022 Gildas Lormeau. All rights reserved.
9483
9730
 
@@ -9546,6 +9793,7 @@
9546
9793
  exports.ERR_UNSUPPORTED_ENCRYPTION = ERR_UNSUPPORTED_ENCRYPTION;
9547
9794
  exports.ERR_UNSUPPORTED_FORMAT = ERR_UNSUPPORTED_FORMAT;
9548
9795
  exports.ERR_WRITER_NOT_INITIALIZED = ERR_WRITER_NOT_INITIALIZED;
9796
+ exports.ERR_ZIP_NOT_EMPTY = ERR_ZIP_NOT_EMPTY;
9549
9797
  exports.HttpRangeReader = HttpRangeReader;
9550
9798
  exports.HttpReader = HttpReader;
9551
9799
  exports.Reader = Reader;